Creating Threads in VB.NET

Today in VB.NET, I will teach you how to create threads. We all know that a thread is a basic processing unit in our computer to which an operating system allocates processor time, and more than one thread can be executing code inside a process. Thus, threads have priority. It is just like a racing program. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Console Application. Note: We will not design any interface because we will program it using vb console application. 2. In your code module, put this code below.
  1. Imports System.Threading
  2. Module Module1
  3.  
  4. Private Sub CarA()
  5. Dim I As Integer
  6.  
  7. For I = 0 To 250
  8. System.Console.Write("Car A" & vbCrLf)
  9. 'delay of 150 ms
  10. Thread.CurrentThread.Sleep(150)
  11. Next
  12. End Sub
  13.  
  14. Sub CarB()
  15. Dim I As Integer
  16.  
  17. For I = 0 To 250
  18. System.Console.Write("Car B" & vbCrLf)
  19. 'delay of 250 ms
  20. Thread.CurrentThread.Sleep(250)
  21. Next
  22. End Sub
  23.  
  24. Sub CarC()
  25. Dim I As Integer
  26.  
  27. For I = 0 To 250
  28. System.Console.Write("Car C" & vbCrLf)
  29. 'delay of 100 ms
  30. Thread.CurrentThread.Sleep(100)
  31. Next
  32. End Sub
  33.  
  34. Public Sub Main()
  35. 'Create threads
  36. Dim A As System.Threading.Thread = New Threading.Thread(AddressOf CarA)
  37. Dim B As System.Threading.Thread = New Threading.Thread(AddressOf CarB)
  38. Dim C As System.Threading.Thread = New Threading.Thread(AddressOf CarC)
  39.  
  40. 'create threads
  41. A.Start()
  42. B.Start()
  43. C.Start()
  44. End Sub
  45.  
  46. End Module
We used the package System.Threading to access the thread class. We have created three methods named CarA, CarB, and CarC that has an I variable that will write their Car names and has a delay of with the sleep method and its time in millisecond. If variable I will reach 250 then the console application will close after executing the threads. Next, in our Main, we have created the three threads using the addresses of the methods that we have created above. And to start the thread, we use the Start method of the thread class. Output: output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment