How do I solve producer-consumer case in a blocking way in Windows Phone?
I work on the application that is kind of a remote controller for a PC (allows to manipulate cursor and keyboard). You can actually see touchpad in the application. When the user performs a gesture on the touchpad, multiple touch events are generated that should be transformed into particular format of network message and sent to the server (running on a PC). The problem is, that they shouldn't be sent by UI thread (as GUI might freeze if the network traffic is large), but a background thread. Also, the order of messages must remain the same as the order of their generation. Normal approach would be to make the UI thread put them into some kind of a blocking queue (like for instance BlockingArrayQueue in Java) and then the background thread would read them from the queue and send to the server one by one. Unfortunately in Windows Phone you do not have access to blocking queues, you don't have access to semaphores either (therefore it is not easy to create a blocking queue). You can get semaphores by implementing Bakery Algorithm, but then again, it's not very efficient and drains the battery.
Re: How do I solve producer-consumer case in a blocking way in Windows Phone?
Which network API do you use? If you have a stream connection and use the XyAsync methods, I think you are pretty much done. Async avoids blocking and stream connection preserves the order.
Re: How do I solve producer-consumer case in a blocking way in Windows Phone?
There are some synchronization primitives available in Windows Phone 7.
There's the [URL="http://msdn.microsoft.com/library/vstudio/system.threading.interlocked(v=vs.95).aspx"]Interlocked class[/URL] and others in the [URL="http://msdn.microsoft.com/library/vstudio/system.threading(v=vs.95).aspx"]System.Threaing namespace[/URL].
There are a few more in the pre-release of [URL="http://nuget.org/packages/Microsoft.Bcl.Async"]Microsoft.Bcl.Async[/URL]. With this package, you can also use the new [URL="http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx"]async/await[/URL] constructs of the C# 5.0 compiler.