Reputation: 33388
I'm in a basic C# .NET 4.0 WinForms application. What i want to do is to write some logic to display a progress bar on the GUI thread while executing background operations on a background thread.
Android has implemented AsyncTask class which has some methods that need override: onPreExecute, doInBackground, onProgressUpdate and onPostExecute. OnPreExecute you can show the progress bar and on PostExecute (when the doInBackground method returns) you can hide it.
I read here that .NET 4.5 comes with something similar built in. Is there anything similiar in .NET 4.0? If not, is it a good idea to implement something like android's AsyncTask or is there a more conventional way to do this in C#?
Regards,
Dan
Upvotes: 2
Views: 975
Reputation: 93
You can create your own abstract class by using BackgroundWorker as described here AsyncTask using BackgroundWorker
Upvotes: 0
Reputation: 9566
Take a look at BackgroundWorker class. It provides all the functionality you described.
Upvotes: 1
Reputation: 62439
Closest thing in .NET would be the BackgroundWorker
class, using these events:
DoWork
- Occurs when RunWorkerAsync
is called.ProgressChanged
- Occurs when ReportProgress
is called.RunWorkerCompleted
- Occurs when the background operation has completed, has been canceled, or has raised an exception.Upvotes: 3