Pages

Sunday, January 1, 2012

Worker Thread ?

0 comments


A worker thread is commonly used to handle background tasks so that user shouldn't have to wait for them to complete and can continue using application.

  • While performing a long running task or a relatively heavy operation, what often happens is that your application’s UI will freeze until the operation completes. This is usually unacceptable because your application’s end user will think it crashed and probably try to close it. Then to avoid making the UI block in these kinds of programs you need to run your heavy operation on a separate thread from that of your UI. To do so we use a background or worker thread to do the work (asynchronously to the primary thread), and then present the results back to the primary thread to consume. This way you will be able to run your operation while also keep the end user informed of the progress from the UI.
  • Generally the term worker (or background) thread is used to describe another thread from the one that isn't doing the work on the current thread - which in lots of cases is a foreground or UI thread. Windows programs generally use a single primary thread to manage the UI and this is generally synchronous (i.e. things runs one after the other).
  • Worker threads are an elegant solution to a number of problems about concurrent processing for example, the need to keep the GUI active while a computation is being performed. In windows programs this is done via messages. If you use particular libraries such as say the .net framework then special utility classes such as ThreadPool and BackgroundWorker are available to make background or worker thread handling easier.
  • Tasks such as recalculation and background printing are good examples of worker threads.

Leave a Reply