Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to thread work in c#?

What's the best way to thread work (methods) in c#?

For example:

Let's say I have a form and want to load data from db.

My form controls: 
 - dataGridView (to show data from DB), 
 - label (loading status) and 
 - button (start loading).

When I click the button my form is frozen until the task is done. Also the loading status does not change until task is done. I think async threading would be the answer?

So my question: what's the best way to handle this? I know there is a lot stuff about Threading, but what's the difference between them and how do you make it thread safe?

How do you solve this kind of problems?

Best Regards.

like image 431
Jooj Avatar asked Dec 23 '22 07:12

Jooj


1 Answers

If using Windows Forms, you should look at BackrgroundWorker. More generally, it is often useful to use the ThreadPool class. And finally, it is worth to take a look at the new .NET 4's Parallel class.

like image 123
Konamiman Avatar answered Jan 15 '23 16:01

Konamiman