Thứ Ba, 30 tháng 8, 2016

VB.NET Tutorial : How to Create Multithreading Example in VB.NET?


MultiThread Example - How to create Multi Threads Example in vb.net? Multithreading used to run many threading process at one times, for example on our computer, the task manager can be seen in the number of processes that occur during our computer is turned on or running.

Pleaser read :
  1. MultiThreading Client Server Chat
  2. MultiThreading with Background Worker

Here we will try to explain and sure you can download MultiThreading source code at the last post lines.

Multithreading Example in VB.NET

Create new project with your visual studio and create with project name "SimpleMultiThread", and at the form1.vb, add 3 buttons, Labels and ProgressBar Component. Design the form like this one :

Multithreading Example in VB.NET

Source Code Multithreading Example VB.NET

Copy all source code below:

Imports System.Threading ' import threading namespaces
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
    End Sub

    Sub Thread1()
        ' declare a variable as integer
        Dim a As Integer = 0
        For x As Integer = 1 To 1000
            a = x * 100 / 1000
            ' loops the progressbar value & the label value
            ProgressBar1.Value = a
            Label1.Text += 1
        Next
    End Sub
    Sub Thread2()
        ' declare a variable as integer
        Dim a As Integer = 0
        For x As Integer = 1 To 2000
            a = x * 100 / 2000
            ' loops the progressbar value & the label value
            ProgressBar2.Value = a
            Label2.Text += 1
        Next
    End Sub
    Sub Thread3()
        ' declare a variable as integer
        Dim a As Integer = 0
        For x As Integer = 1 To 3000
            a = x * 100 / 3000
            ' loops the progressbar value & the label value
            ProgressBar3.Value = a
            Label3.Text += 1
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'we will declare Multithread1 will be load from function Thread1
        Dim MultiThread1 As New Thread(AddressOf Thread1)
        ' declare the ProgressBar Value = 0
        ProgressBar1.Value = 0
        ' Declare the Label1 value = 0
        Label1.Text = 0
        ' start the threading
        MultiThread1.Start()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'we will declare Multithread1 will be load from function Thread1
        Dim MultiThread2 As New Thread(AddressOf Thread2)
        ' declare the ProgressBar Value = 0
        ProgressBar2.Value = 0
        ' Declare the Label1 value = 0
        Label2.Text = 0
        ' start the threading
        MultiThread2.Start()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        'we will declare Multithread1 will be load from function Thread1
        Dim MultiThread3 As New Thread(AddressOf Thread3)
        ' declare the ProgressBar Value = 0
        ProgressBar3.Value = 0
        ' Declare the Label1 value = 0
        Label3.Text = 0
        ' start the threading
        MultiThread3.Start()
    End Sub
End Class

Download Full source code MultiThreading vb.net

if you still confused, just watch video below.

Video tutorial How to create Multiple Threads in vb.net


See you Next Lessons

Không có nhận xét nào:

Đăng nhận xét