Usually when you're code is doing anything slow, everything has to wait for it to get finished before they can do anything, this will happen when our application is single threaded. To solve this we can add a backgroundworker object, you can found them in the toolbox. If you want to follow the tutorial by doing the same as I'll tell you, then create a background worker called "Worker1".
In this example, We want to be able to report progress and cancel the worker when we want, then we want to start it. To do this we do like this:
So now the working will start to work, so now it will call its DoWork event which will be in another thread so it won't interrupt anything else. Since this is only examples to teach you, this thread will loop through an empty for loop one billion times, just to let it do anything that takes some time. For me it takes 4 seconds but If you have a slow computer you can lower the amount of times the loop will loop:Code:Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Worker1.WorkerReportsProgress = True
Worker1.WorkerSupportsCancellation = True
Worker1.RunWorkerAsync()
End Sub
Code:Private Sub Worker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker1.DoWork
For i As Long = 1 To 1000000000
Next
End Sub
Normally when an application is doing something which takes some time to finish and you try to interact with it by for example clicking on it, it will stop to respond, which isn't the case now since the time taking action is done in another thread.
When the backgroundworking is done we often wants to do something, we can then use the RunWorkerCompleted event to do this. In this example we'll just show a messagebox saying it's done:
So why don't we just do this in the end of the DoWork event? The reason is that that event is working in another thread and therefor can't access things from another thread, you can still access variables from outside the special thread and the above example would work fin inside the other thread but if we for example want to alter anything on the form we can't, since that is created by another thread. So if we added this at the end of DoWork(inside the out other thread):Code:Private Sub Worker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles Worker1.RunWorkerCompleted
MessageBox.Show("Worker1 is done!")
End Sub
we would receive this error:Code:Me.Text = "Worker1 is done!"
To solve this we do as above, using the RunWorkerCompleted event since that will be run in our main thread and can therefor alter for example our main form.Code:Cross-thread operation not valid: Control 'frmMain' accessed from a thread other than the thread it was created on.
In the same way we maybe want to alter something from another thread inside our background worker, to do this we have to use the ProgressChanged event. If we alter the DoWork event to this:
We'll then on some values(1, 250 millions, 500 millions, 750 millions and 1 billion) report the progress (0%, 25%, 50%, 75% and 100%). This can be used in the ProgressChanged event like this:Code:Private Sub Worker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker1.DoWork
For i As Long = 1 To 1000000000
Select Case i
Case 1
Worker1.ReportProgress(0)
Case 250000000
Worker1.ReportProgress(25)
Case 500000000
Worker1.ReportProgress(50)
Case 750000000
Worker1.ReportProgress(75)
Case 1000000000
Worker1.ReportProgress(100)
End Select
Next
End Sub
So then we'll get the Progress done outside the special thread so we can use it in our main thread.Code:Private Sub Worker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles Worker1.ProgressChanged
Me.Text = e.ProgressPercentage & "% done"
End Sub
That was everything for this tutorial about multi threading, hope you enjoyed it.![]()
Last edited by James.H; 03-05-2010 at 10:04 AM.
Nicely done. How often do you use these workers in your code?
+rep
I actual don't use them that often because I don't make that kind of programs so often. But from time to time I use them.![]()
Threading... gotta love/hate it+rep
Great stuff!
Great info, helped me a bit in a few issues i'm having.
The only issues i have with multi-threading is you have to have the end result data carrier such as a list-box or text-box as a public control :-/
which makes security fall apart for decompliers that try to rip off your software but i sapose you cant avoid that these days.
Great stuff though, +Rep
I found a simple definition on another site so i prefered to write it here not because this tut is bad " it is really wonderful " but for more details:
What is Multithreading
Multithreading is a feature provided by the operating system that enables your application to have more than one execution path at the same time. We are all used to Windows' multitasking abilities, which allow us to execute more than one application at the same time. Just right now I am writing the 14th lesson of the Programmers Heaven's VB.NET School in Microsoft Word, listening to my favorite songs in WinAmp and downloading a new song using the Internet Download Manager. In a similar manner, we may use multithreading to run different methods of our program at the same time. Multithreading is such a common element of today's programming that it is difficult to find windows applications that don't use it. For example, Microsoft Word takes user input and displays it on the screen in one thread while it continues to check spelling and grammatical mistakes in the second thread, and at the same time the third thread saves the document automatically at regular intervals. In a similar manner, WinAmp plays music in one thread, displays visualizations in the second and takes user input in the third. This is quite different from multitasking as here a single application is doing multiple tasks at the same time, while in multitasking different applications execute at the same time.
Your code doesn't work at all.
There are currently 2 users browsing this thread. (0 members and 2 guests)
Bookmarks