Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Threads in Windows Phone

So far during my experience in Windows Phone 7 application development I notices there are different ways to runs an action in an asynchronous thread.

  1. System.Threading.Thread
  2. System.ComponentModel.BackgroundWorker
  3. System.Threading.ThreadPool.QueueUserWorkItem()

I couldn't see any tangible difference between these methods (other than that the first two are more traceable).

Is there any thing you guys consider before using any of these methods? Which one would you prefer and why?

like image 774
Mo Valipour Avatar asked Nov 08 '11 21:11

Mo Valipour


People also ask

What are background threads?

In programming, a background thread is a thread that runs behind the scenes, while the foreground thread continues to run. For instance, a background thread may perform calculations on user input while the user is entering information using a foreground thread.

What is background thread in Android?

Background processing in Android refers to the execution of tasks in different threads than the Main Thread, also known as UI Thread, where views are inflated and where the user interacts with our app.

What is foreground and background thread?

Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.

What is main thread and background thread?

In Android there is a concept of the Main Thread or UI Thread. If you're not sure what a thread is in computer science, check out this wikipedia article. The main thread is responsible for keeping the UI running smoothly and responding to user input. It can only execute one task at a time.


3 Answers

The question is kinda answered but the answers are a little short on detail (IMO).

Lets take each in turn.

System.Threading.Thread

All the threads (in the CLR anyway) are ultimately represented by this class. However you probably included this to query when we might want to create an instance ourselves.

The answer is rarely. Ordinarily the day-to-day workhorse for dispatching background tasks is the Threadpool. However there are some circumstances where we would want to create our own thread. Typically such a thread would live for most of the app runtime. It would spend most of its life in blocked on some wait handle. Occasionally we signal this handle and it comes alive to do something important but then it goes back to sleep. We don't use a Threadpool work item for this because we do not countenance the idea that it may queue up behind a large set of outstanding tasks some of which may themselves (perhaps inadverently) be blocked on some other wait.

System.ComponentModel.BackgroundWorker

This is friendly class wrapper around the a ThreadPool work item. This class only to the UI oriented developer who occasionally needs to use a background thread. Its events being dispatched on the UI thread makes it easy to consume.

System.Threading.ThreadPool.QueueUserWorkItem

This the day-to-day workhorse when you have some work you want doing on a background thread. This eliminates the expense of allocating and deallocating individual threads to perform some task. It limits the number of thread instances to prevent too much of the available resources being gobbled up by too many operations try to run in parallel.

The QueueUserWorkItem is my prefered option for invoking background operations.

like image 149
AnthonyWJones Avatar answered Sep 29 '22 08:09

AnthonyWJones


It arguably depends on what you are trying to do, you have listed 3 very different threading models.

  1. Basic threading
  2. Designed for applications with a seperate UI thread.
  3. Managed thread pool

Have you read MSDN etc...

http://www.albahari.com/threadin

Http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx

like image 26
Lloyd Avatar answered Sep 29 '22 08:09

Lloyd


You don't state "what for", but

  1. Basic Thread - quite expensive, not for small jobs
  2. Backgroundworker - mostly for UI + Progressbar work
  3. ThreadPool - for small independent jobs

I think the TPL is not supported in SL, which is a pity.

like image 35
Henk Holterman Avatar answered Sep 29 '22 08:09

Henk Holterman