Thursday, May 23, 2013

Example of thread synchronization in VB.NET

Example of thread synchronization


Refers of the idea of using multiples thread in a process



When you stats using thread, you will notice that the speed of each thread is not very important. You will notice that synchronization is more important because sometimes, you want a thread to be completed before starting and other one. You could try to use or check for the Thread status using the IsAlive and IsBackground properties, but you will find it mostly hard to get reliable things.
That is why synchronization between asynchronous tasks is the key for success. If you master the basic of timing between functions, nothing will stop you from doing more complex application or games.

MMORPG (stands for Massive Multi Online Role Player Game) is the perfect example of challenge for a programmer or a team where each player has different computer speed and network performance.

HereVB a code sample I made in less than 15 minutes. Yes, it is very simple. The concept is also simple. I made a WinForm using 4 ProgressBar. The last ProgressBar will only start if the 3 previous have completed. In other words, the last ProgressBar must wait for the 3 others.

This sample could be use in any situation in the modern world.

By pressing the button START, 4 independent threads will start. Each Thread has their speed.

The last Thread is blocked by 3 ManualResetEvent. If the 3 ManualResetEvent are set, then the last task could start.






''' <summary>
''' Author : Check-Kay Wong
''' Description:
''' This is a sample of synchronasation between multiples task
''' using a Simple WaitOne and a Set from a ManualResetEvent
'''
''' Comment : this is an exemple
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Private tTask1 As System.Threading.Thread
    Private tTask2 As System.Threading.Thread
    Private tTask3 As System.Threading.Thread
    Private tTask4 As System.Threading.Thread

    Private mreTask1 As System.Threading.ManualResetEvent
    Private mreTask2 As System.Threading.ManualResetEvent
    Private mreTask3 As System.Threading.ManualResetEvent
    Private mreTask4 As System.Threading.ManualResetEvent

    Private Delegate Sub dTask1()
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        StartAllTasks()
    End Sub
    ''' <summary>
    ''' all working stuff inside the thread are simulated by progressbar
    ''' </summary>
    ''' <remarks></remarks>
#Region "Working Thread 1, 2 and 3"
    Private Sub Task1()
        Do
            Me.BeginInvoke(New dTask1(AddressOf sTask1))
            System.Threading.Thread.Sleep(100)
        Loop Until ProgressBar1.Value >= ProgressBar1.Maximum
        mreTask1.Set()
    End Sub
    Private Sub sTask1()
        Me.ProgressBar1.PerformStep()
    End Sub

    Private Sub Task2()
        Do
            Me.BeginInvoke(New dTask1(AddressOf sTask2))
            System.Threading.Thread.Sleep(100)
        Loop Until ProgressBar2.Value >= ProgressBar2.Maximum
        mreTask2.Set()
    End Sub
    Private Sub sTask2()
        Me.ProgressBar2.PerformStep()
    End Sub

    Private Sub Task3()
        Do
            Me.BeginInvoke(New dTask1(AddressOf sTask3))
            System.Threading.Thread.Sleep(100)
        Loop Until ProgressBar3.Value >= ProgressBar3.Maximum
        mreTask3.Set()
    End Sub
    Private Sub sTask3()
        Me.ProgressBar3.PerformStep()
    End Sub
#End Region
#Region "Finale Task Thread"
    Private Sub Task4()
        mreTask1.WaitOne()
        mreTask2.WaitOne()
        mreTask3.WaitOne()
        Do

            Me.BeginInvoke(New dTask1(AddressOf sTask4))
            System.Threading.Thread.Sleep(100)
        Loop Until ProgressBar4.Value >= ProgressBar4.Maximum
        mreTask4.Set()
        My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep) 'a little bell
    End Sub
    Private Sub sTask4()
        Me.ProgressBar4.PerformStep()
    End Sub
#End Region

    ''' <summary>
    ''' Init stuff
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.Text = My.Application.Info.ProductName & " from " & My.Application.Info.CompanyName
        ProgressBar1.Maximum = 200
        ProgressBar2.Maximum = 100
        ProgressBar3.Maximum = 140
        ProgressBar4.Maximum = 240

        'create the ManualResetEvent
        mreTask1 = New System.Threading.ManualResetEvent(False)
        mreTask2 = New System.Threading.ManualResetEvent(False)
        mreTask3 = New System.Threading.ManualResetEvent(False)
        mreTask4 = New System.Threading.ManualResetEvent(False)
    End Sub

    ''' <summary>
    ''' Start All Task at the same time
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub StartAllTasks()
        If tTask1 IsNot Nothing Then tTask1 = Nothing
        tTask1 = New System.Threading.Thread(AddressOf Task1)
        tTask1.IsBackground = True
        tTask1.Start()

        If tTask2 IsNot Nothing Then tTask2 = Nothing
        tTask2 = New System.Threading.Thread(AddressOf Task2)
        tTask2.IsBackground = True
        tTask2.Start()

        If tTask3 IsNot Nothing Then tTask3 = Nothing
        tTask3 = New System.Threading.Thread(AddressOf Task3)
        tTask3.IsBackground = True
        tTask3.Start()

        If tTask4 IsNot Nothing Then tTask4 = Nothing
        tTask4 = New System.Threading.Thread(AddressOf Task4)
        tTask4.IsBackground = True
        tTask4.Start()
    End Sub

End Class


I use Microsoft Visual Studio 2010 to do all my projects. I suggest you to buy and get the best IDE in the world and the newest one: Microsoft Visual Studio 2012 on Amazon
or
Download the sample code here: ManualResetEventExample.zip

Others related post:

No comments:

Post a Comment