ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

40- 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.
  1. Dim i As Integer
  2. Dim i2 As Integer
  3.  
  4. Dim Mthread As System.Threading.Thread
  5. Dim Mthread2 As System.Threading.Thread
  6.  
  7. Private Sub incValue()
  8. Do Until i = 100
  9. i = i + 1
  10. TextBox1.Text = i
  11. Me.Refresh()
  12. Loop
  13. End Sub
  14.  
  15. Private Sub incValue2()
  16. Do Until i2 = 100
  17. i2 = i2 + 1
  18. TextBox2.Text = i2
  19. Me.Refresh()
  20. Loop
  21. End Sub
  22.  
  23. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  24. Mthread = New System.Threading.Thread(AddressOf incValue)
  25. Mthread.Start()
  26. End Sub
  27.  
  28. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  29. Mthread2 = New System.Threading.Thread(AddressOf incValue2)
  30. Mthread2.Start()
  31. End Sub
  32.  
  33. Private Sub multithread_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  34. Control.CheckForIllegalCrossThreadCalls = False
  35. End Sub

Output.
multithreadF2

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme