Background process in vb.net
This month i started a new software, it’s a SEO Analisys Tool, and i needed to threat multithreading and background operations handling. Giving a look inside the .Net Framework i found a very interesting class, the BackgroundWorker class. First of all i have to tell you where it’s placed, in most articles on the web they forget to tell the reader where he can find it. It’s placed into the System.ComponentModel namespace.
The BackgroundWorker class is a really simple to use class, it gives you the opportunity to handle multitasking in a very easy way.
Basic informations about the class
The BackgroundWorker class exposes just few events to control the status of the process an to notify what’s going on.
- DoWork (in this you’ll write all the code to execute)
- ProgressChanged (raised when the thread execution process changes)
- RunWorkerCompleted (when the thread job is finished)
All that you have to know is
- On the top of your code page include the System.ComponentModel namespace (imports System.ComponentModel)
- In the DoWork event write your code loop and remember to throw the ProgressChanged event if you need to handle it (RaiseEvent ProgressChanged(me)
- Handle the process cancellation in the DoWork loop checking for CancellationPending flag (if bgWork.CancellationPending then e.cancel)
- if you need the thread cancellation support you have to add a line of code to the New sub (bgWork.WorkerSupportsCancellation = true)
I know it seems too easy to be true but it’s really so…
Here you can find a sample piece of code to get download progress in vb.net
TAGS: Asp.net, dotnet, code snippet, seo, search engine optimization, visual studio.net, sample code, c#, vb.net

