Apps can be multi-threaded

May 21, 2010 10:32 GMT  ·  By

Windows Phone 7, the next flavor of Microsoft's mobile operating system, was previously reported to lack one of the most appealing features its predecessors had, namely multi-tasking, yet it seems that things are a little different. When launched on the market, Window Phone 7 would not offer multi-tasking for 3rd party applications, yet software solutions have the possibility to get background processing time when the system is not overloaded. In the end, it all comes down to the manner in which applications are written, as a recent post on Doug Boling's blog notes.

The idea is that Windows Phone applications can be multi-threaded, where the main thread of the software solution is the user interface thread, while other threads called by the app won't be able to interact with the UI, being called “worker threads” or “background threads.” As soon as the application is sent to the background, the foreground thread is suspended, while worker threads continue to run.

“These worker threads can call base class library methods such as querying the file system, reading and writing files, and such even while the application is 'suspended.' When a worker thread tries to invoke a method on the foreground thread, that request is queued but not executed. When the application is resumed, the queued calls are invoked,” is what the aforementioned blog post states. However, it seems that suspended applications can still run in case the system is lightly loaded, otherwise they will be terminated.

On the other hand, he notes that running applications won't be terminated when the user hits the Windows or Search buttons, but that they will be suspended. When hitting the back button, the app resumes, as the two events that are fired here are WindowsPhoneEvents.Pause and WindowsPhoneEvents.Resume, and not Application_Exit or Application_Startup. Developers will need to add handlers for these events in their solutions.

Doug Boling concludes, “while true multitasking doesn’t exist on the Windows Phone, applications can ‘borrow’ some background processing time as long as the system is lightly loaded. […] However, developers should understand this “feature” and if their applications have worker threads that don’t need to run while the application is suspended, they need to ensure that those “worker” threads block while the application is suspended.”