Visual Basic
Below are a few examples and basic introductions to some core Visual Basic programming concepts and topics. This page hopes to become a reference or library for some basic VB functions and concepts.
Visual Studio 2012 can be downloaded as a trial from Microsoft here.
Visual Studio 2012 can be downloaded as a trial from Microsoft here.
Multi-Threading
Adding multiple working threads into your application will allow you to process more than one task at a time saving time having to wait for one task to complete before another one can start. There are two ways to apply multi-threading to an application. The first way, the easy way, is to simply add the backgroundworker() component to your application and have events handled by your background worker. The other way to employ multiple threads is to invoke objects to run in a separate thread. This is slightly more complicated but a better method is you are working more with objects.
BackgroundWorker()
BackgroundWorker()
Add the following to a blank form:
3 Buttons (Button1, Button2, Button3) 4 Lables (lbl1, lbl2, lbl3, lblTime) 4 BackgroundWorkers (BackgroundWorker1, BackgroundWorker2, BackgroundWorker3, BackgroundWorkerTime) 1 Timer (TimerTimeShow) Declare a datetime variable called timenow under the public class for the form. Dim timenow As DateTime |
1. First set the property for the timer to enabled with an interval of 1000 (ms).
2. Double click into your backgroundworker1. This will bring up the sub for the background worker event handler. In here you can put any tasks you would like to complete. Reference to objects in here with a cross thread exception. To work with objects it is best to invoke them on a separate thread.
For the example I have simply told the thread to sleep for a number of seconds.
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
' Events to execute on the backgroundworker1 thread go here.
System.Threading.Thread.Sleep(4000)
End Sub
3. Do this also for BackgroundWorker2 and BackgroundWorker3 too:
Private Sub BackgroundWorker2_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
System.Threading.Thread.Sleep(3000)
End Sub
Private Sub BackgroundWorker3_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker3.DoWork
System.Threading.Thread.Sleep(2000)
End Sub
4. BackgroundWorkers have a RunWorkerCompleted handler. This is called once the background worker has completed the tasks in its DoWork handler. The RunWorkerCompleted handler is usefull for calls to objects such as lables.
Private Sub BackgroundWorker1_WorkCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
lb1.Text = "Thread Finished"
End Sub
Private Sub BackgroundWorker2_WorkCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
lbl2.Text = "Thread Finished"
End Sub
Private Sub BackgroundWorker3_WorkCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker3.RunWorkerCompleted
lbl3.Text = "Thread Finished"
End Sub
5. The final background worker - BackgroundWorkerTime.
Make sure to declare the timenow datetime variable under the public class for the form. This background worker will simply set that variable to the current time.
Private Sub BackgroundWorkerTime_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerTime.DoWork
timenow = Now
End Sub
Private Sub BackgroundWorkerTime_WorkCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerTime.RunWorkerCompleted
lblTime.Text = Now
End Sub
6. Now we have the code for the backgroundworkers but as of yet have not called them to start working. Click into the buttons to bring up the handlers for the click events. Behind the handlers we call the background workers to start with BackgroundWorker1.RunWorkerAsync() :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
lbl1.Text = "Thread Started"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BackgroundWorker2.RunWorkerAsync()
lbl2.Text = "Thread Started"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
BackgroundWorker3.RunWorkerAsync()
lbl3.Text = "Thread Started"
End Sub
7. Now lastly for the Timer. Double click into the timer to bring up the timer.tick handler.
Private Sub TimerTimeShow_Tick(sender As Object, e As EventArgs) Handles TimerTimeShow.Tick
If Not BackgroundWorkerTime.IsBusy Then
BackgroundWorkerTime.RunWorkerAsync()
Else
lblTime.Text = Now
End If
End Sub
Here I have used an IF statement to check if the background working is busy running any tasks before i call it. If it is not running any tasks it is called to run else the current time is set for the lable and displayed anyway.
8. Run the application and click the three buttons. Each thread will run seperatly, wait there time with System.Threading.Thread.Sleep() and then when they have finished display lbl1.Text = "Thread Finished". The timer thread however continuously is running in the background every 1 second.
2. Double click into your backgroundworker1. This will bring up the sub for the background worker event handler. In here you can put any tasks you would like to complete. Reference to objects in here with a cross thread exception. To work with objects it is best to invoke them on a separate thread.
For the example I have simply told the thread to sleep for a number of seconds.
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
' Events to execute on the backgroundworker1 thread go here.
System.Threading.Thread.Sleep(4000)
End Sub
3. Do this also for BackgroundWorker2 and BackgroundWorker3 too:
Private Sub BackgroundWorker2_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
System.Threading.Thread.Sleep(3000)
End Sub
Private Sub BackgroundWorker3_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker3.DoWork
System.Threading.Thread.Sleep(2000)
End Sub
4. BackgroundWorkers have a RunWorkerCompleted handler. This is called once the background worker has completed the tasks in its DoWork handler. The RunWorkerCompleted handler is usefull for calls to objects such as lables.
Private Sub BackgroundWorker1_WorkCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
lb1.Text = "Thread Finished"
End Sub
Private Sub BackgroundWorker2_WorkCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
lbl2.Text = "Thread Finished"
End Sub
Private Sub BackgroundWorker3_WorkCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker3.RunWorkerCompleted
lbl3.Text = "Thread Finished"
End Sub
5. The final background worker - BackgroundWorkerTime.
Make sure to declare the timenow datetime variable under the public class for the form. This background worker will simply set that variable to the current time.
Private Sub BackgroundWorkerTime_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerTime.DoWork
timenow = Now
End Sub
Private Sub BackgroundWorkerTime_WorkCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerTime.RunWorkerCompleted
lblTime.Text = Now
End Sub
6. Now we have the code for the backgroundworkers but as of yet have not called them to start working. Click into the buttons to bring up the handlers for the click events. Behind the handlers we call the background workers to start with BackgroundWorker1.RunWorkerAsync() :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
lbl1.Text = "Thread Started"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BackgroundWorker2.RunWorkerAsync()
lbl2.Text = "Thread Started"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
BackgroundWorker3.RunWorkerAsync()
lbl3.Text = "Thread Started"
End Sub
7. Now lastly for the Timer. Double click into the timer to bring up the timer.tick handler.
Private Sub TimerTimeShow_Tick(sender As Object, e As EventArgs) Handles TimerTimeShow.Tick
If Not BackgroundWorkerTime.IsBusy Then
BackgroundWorkerTime.RunWorkerAsync()
Else
lblTime.Text = Now
End If
End Sub
Here I have used an IF statement to check if the background working is busy running any tasks before i call it. If it is not running any tasks it is called to run else the current time is set for the lable and displayed anyway.
8. Run the application and click the three buttons. Each thread will run seperatly, wait there time with System.Threading.Thread.Sleep() and then when they have finished display lbl1.Text = "Thread Finished". The timer thread however continuously is running in the background every 1 second.