Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# / VB.Net Task vs Thread vs BackgroundWorker

I have "Googled" but still confused with Task, Thread, and Background Worker.....

  1. Is "Task is a high level API that running on current thread" correct ?

  2. If 1 is correct, why I need to use invoke to change the UI inside the task at same thread ?

  3. Backgroundworker only got lowest priority in the application ? So the performance of backgroundworker is lower than task and thread ? Right ?

  4. Finally, in my application I need to get a string from server using "HttpWebRequest", after that parse the string and update the UI. If I use "HttpWebRequest.BeginGetResponse" to wait for the async result and trigger an complete event to update the UI, I need to use invoke method to call the UI thread control, but can I use background worker instead of ? I can just simply change the UI in "RunWorkerCompleted" event, are there any disadvantage ?

Sorry for my pool English and thanks for help...!

like image 515
Rex Lam Avatar asked Sep 18 '12 18:09

Rex Lam


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

1) No, a Task is by default run on a thread pool thread. You can provide another scheduler which can run tasks differently, though.

3) There is no difference in priority by default. BackgroundWorker also runs on a thread pool thread.

4) Using TaskFactory.FromAsync is a rather simple way to handle asynchronous web requests:

Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null)
    .ContinueWith(
        t =>
        {
            using (var response = (HttpWebResponse)t.Result)
            {
                // Do your work
            }
        },
        TaskScheduler.FromCurrentSynchronizationContext()
    );

Using TaskScheduler.FromCurrentSynchronizationContext ensures that the callback in ContinueWith is invoked on the current thread. So, if the task is created on the UI thread, the response will be retrieved in the background and then processed on the UI thread.

like image 137
Bojan Resnik Avatar answered Sep 28 '22 21:09

Bojan Resnik