This is a mirror of official site: http://jasper-net.blogspot.com/

Threading recipes: the many ways of spawning background works in Silverlight

| Thursday, May 19, 2011
People often think at Silverlight like a beautiful toolset useful to create amazing interfaces on the web. Since it is true for sure that it does not exists a similar toolset that join power and simplicity like Silverlight does, it's already true that under the cover of the user interface it beats a tiger heart that is capable of features that are typically reserved to desktop frameworks. As an example, a respectful application framework can't play its role without the capability of processing background operations and from this point of view Silverlight is not second to any other.

Processing background operations is important for a number of reasons; first of all, since we are developing application that have an user interface it is important to process things without locking the UI. When you call the network this is implicitly done but there is many cases you have to do this by hand for your particular purposes. Secondary it is important to be able to optimize the computation when you have an huge number of elements process. From this point of view the threading helps you to improve the performances of the application balancing the work across multiple threads. Finally there is many times when you need to run scheduled operations - for instance when you need to poll the network - and being able to spawn a thread responsible of scheduling the activities is important to better organize the application.

Silverlight contains an huge set of threading tools and sometimes it is difficult to choose from one or the other, and also often at the way you chosen for your purpose, it corresponds another way that is better tailored for your needs.

Concurrency and Marshaling: the two edges of the threading blade
Before starting to explain some of the ways of spawning a background thread, is it required to spend some words about what you have to be strongly aware when you start writing in a multithreaded way. As you will see there is lot of opportunities for threading but the very difficult thing is not starting the background operation but usually dealing with concurrency and marshaling.

Concurrency is a concept so hard to deal with almost as simple it is to be explained. Imagine you have a resource shared by two persons that may access and change it, you need to be careful that the manipulation made by one of the two actors does not have impact with the work that is done by the other side. Explaining concurrency properly is out of the scope of this article since it probably will take lot of pages. Suffice is to understand that Silverlight has a number of tools that helps you to deal with concurrency. Lock, Monitor, Interlocked, ManualResetEvent, AutoResetEvent and WaitHandles are all types that in a way or in another can be used to manage access to shared resources. Someone is specifically drafted for the purpose of synchronizing threads (Lock, Monitor and Interlocked) and other are supporting tools that enable waiting across threads (events and WaitHandles). I suggest you to spent some time trying to understand how they works just a minute after the end of this reading and for sure before start writing a single line of code.

Read more: Silverlight show

Posted via email from Jasper-net

0 comments: