40- MultiThreading

0
386

This Visual Basic tutorial is all about MultiThreading.
Multithreading (or free threading) can execute multiple tasks simultaneously  in the Visual Basic applications. This is a process of individual tasks that execute on separate threads. Multithreading enhances the performance and responsiveness of your applications.

 

Example:

  • Open the Visual Basic, select “File” on the menu, hit new and create a new project.
    01_helloworld
  • The New Project dialog will appear.
    create2s
    Select “windows” in the project types, hit the “windows form application” in the templates and hit “ok“.
  • Add two buttons and two textboxes in the form.
    multithreadF1
  • Add the following code in the code editor.
[vb] Dim i As Integer
Dim i2 As Integer

Dim Mthread As System.Threading.Thread
Dim Mthread2 As System.Threading.Thread

Private Sub incValue()
Do Until i = 100
i = i + 1
TextBox1.Text = i
Me.Refresh()
Loop
End Sub

Private Sub incValue2()
Do Until i2 = 100
i2 = i2 + 1
TextBox2.Text = i2
Me.Refresh()
Loop
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Mthread = New System.Threading.Thread(AddressOf incValue)
Mthread.Start()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Mthread2 = New System.Threading.Thread(AddressOf incValue2)
Mthread2.Start()
End Sub

Private Sub multithread_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub

[/vb] Output.
multithreadF2

 

Readers might read also:

 

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.