Dealing with file locking on Windows Phone 8

I continue to have two issues with my first Windows Phone 8 app, Software Serial Manager. First, the ads don’t always work. Second, about 50% of the time, the app crashes on startup because the data file is in use.

Ads on WP8:
I wrote about how I added an ad control to this app. What’s weird is that it worked for me on my phone, most of the time. However, ads never worked for anyone else. After talking with colleague Binoj, he suggested making sure I am handling the Error_Occurred event for the control. Even just a blank event handler should do. So, I wired up the event to an empty event handler:


So, we’ll see if that works. This will go out in this next release. Unfortunately, you can’t test ads in the emulator, you need to release the app to test it!

File locking:
What a pain this turns out to be. On Windows Phone, EVERYTHING has to be asynchronous. “Great!”, you say. Well, that is unless you actually WANT things to happen in a specific, synchronous order. In this case, it looks like the Load and Save process which happens right as the app starts, are stepping on each other. That results in a crash, and the screen goes away. The user re-launches, and usually in 1-3 times, the app eventually loads fine.

Luckily, I was able to reproduce this in Visual Studio with the emulator, so I could model the problem, and potential solutions. Since everything uses async/await, my first thought was to just use a regular “lock” keyword. If you are not familiar, this is a mechanism you can use to synchronize two functions. The first method “locks” the object; the second method, checks to see if it’s locked, if it is it waits, then goes, and IT then locks. So, this is a simple mechanism to make sure that operations don’t step on each other.

This is almost the same as a “talking stick” – whoever is holding the stick, gets to talk. In our case, whoever has the lock, gets to use the file, when you’re done, you give up the lock and someone else takes it.

In code, when I actually tried to implement this – I got a red-squiggly that the “await” keyword is not allowed within a lock() {..} statement. Why? So, I googled on Bing, and I found this:

Well, considering I can’t use the “lock” keyboard, and I *KNOW* I can control the locking mechanism, I created a poor-man’s-lock like this, for load and save:


that isFileInUse is a Boolean declared as private and static. And then I have a finally block where I set this to false.

Does it work? Yes, every time so far – works like a charm! Is it correct? I don’t think so, this smells very, very bad to me and seems like a major hack. However, since I have to work around this async/await craziness, I have to do something! This is the only thing I came up with.

So, I am going to do some more testing, and if this doesn’t break, this will also ship in this next release.

Posted in Ads, Uncategorized, WindowsPhone

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

%d bloggers like this: