BackgroundWorker Class example in windows forms application

20
1



Link for code samples used in the demo

Link for all dot net and sql server video tutorial playlists

In this video, we will discuss the use of BackgroundWorker Class in windows forms application with an example. Along the way, we will also discuss using progressbar control.

First, let’s understand, why should we use BackgroundWorker Class?
By default windows applications are single threaded. This means, when we run the application there will be a single thread which is commonly called as UI thread. This is the thread that is responsible for doing all the work, that is
1. Creating and updating user interface elements and
2. Executing application code

For example, let’s say we have a function that takes 10 seconds to complete. While the UI thread is busy processing this function, the UI of the application is frozen and the end user cannot interact with the controls on the form during those 10 seconds. To keep our application responsive, we can create a new Thread ourselves and then ask that thread to execute the function that takes long time to complete, so that the UI thread is free to update the controls on the form. But the problem with creating threads ourselves is that, it can get extremely complex especially when we have to update UI controls based on the status of the thread function execution. It’s dangerous to access UI objects on a thread that didn’t create them. So the better option here is to use, BackgroundWorker Class which simplifies multithreading.

Nguồn:https://wijstaanvooronzegrondrechten.org/

Xem Thêm Bài Viết Khác:https://wijstaanvooronzegrondrechten.org/cong-nghe

20 COMMENTS

  1. Very very nice tutorial. Thank you for the code snippets (i didnt use them but i think they are very helpful). Dislike for the thumps down people.

  2. thank you so much

    your video helped me to solve a big problem in my program

    I did not know how build ground worker is worked

    Your style of explanation is very simple and so easy to understand
    you are the best

  3. There is a story, possibly apocryphal, about Carl Friedrich Gauss as a child in school, adding the numbers between 1 and 100 in his head, almost as quickly as the program in this video does it. His technique is beguilingly obvious, even to the ordinary mind. Yet, it took an extraordinary mind to devise it.

  4. Possible Alternate concepts to achieve dynamic progress bar update:
    1. In Web development, Duplex message pattern using WCF/Web API
    2. Use INotifyChange att with WPF with two way binding.
    3. Use Timer control, and tick the timer with update progress-bar untill 100%.
    4. Iterate using Asynch and await with control bound to the result.

LEAVE A REPLY

Please enter your comment!
Please enter your name here