Today I decided to create a custom workflow with Multithreading in such a way that results from a previous thread that was spawned first is required to complete before the results from the thread’s therefor after are rendered. In the example below I created 10 threads, Everytime Mod 3 = 0 is true, that thread will stay alive longer than the rest that are spawned. In this case, Thread 3,6,9 are all active for 5 seconds while the rest are only alive for a second. The outcome of the code results are as follows
Thread 1 Started! 0Previous Thread is nothing 0: Done! Thread 2
Started! Thread 3 Started! Thread 4 Started! Thread 5 Started! Thread
6 Started! Thread 7 Started! Thread 8 Started! Thread 9 Started!
Thread 10 Started! ‘VBMultiThreading.exe’ (CLR v2.0.50727:
VBMultiThreading.exe): Loaded
‘C:\Windows\assembly\GAC_MSIL\Accessibility\2.0.0.0__b03f5f7f11d50a3a\Accessibility.dll’.
Cannot find or open the PDB file. test came back from0 The thread
0x1ee8 has exited with code 0 (0x0). 1: Done! test came back from1 The
thread 0x5c20 has exited with code 0 (0x0). 2: Done! test came back
from2 The thread 0x6cc4 has exited with code 0 (0x0). 3: Done! test
came back from3 The thread 0x49f8 has exited with code 0 (0x0). 4:
Done! test came back from4 The thread 0x302c has exited with code 0
(0x0). 5: Done! test came back from5 The thread 0x4dd0 has exited with
code 0 (0x0). 6: Done! test came back from6 The thread 0x53a0 has
exited with code 0 (0x0). 7: Done! test came back from7 The thread
0x4bc4 has exited with code 0 (0x0). 8: Done! test came back from8 The
thread 0x41e8 has exited with code 0 (0x0). 9: Done! test came back
from9 The thread 0xa1f4 has exited with code 0 (0x0). 10: Done! test
came back from10 The thread 0x32fc has exited with code 0 (0x0). The
program ‘[21476] VBMultiThreading.exe’ has exited with code 0 (0x0).
As you can see regardless of the workflow or even the order they are created as long as the previous thread is attached to the class it will always wait while the previous thread results are in. This workflow increases performance without losing any flexibility when developing multithreaded applications.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
Public Class Form1 Private Delegate Sub MyDelPtr(CurrentInstance As MyThreading) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim MyThreadingClass As MyThreading MyThreadingClass = New MyThreading("0", "1", Nothing, Me, New MyDelPtr(AddressOf Test)) For i = 1 To 10 If i Mod 3 = 0 Then MyThreadingClass = New MyThreading(i, "5", MyThreadingClass.MyThreadToStart, Me, New MyDelPtr(AddressOf Test)) Else MyThreadingClass = New MyThreading(i, "1", MyThreadingClass.MyThreadToStart, Me, New MyDelPtr(AddressOf Test)) End If Debug.WriteLine("Thread " & i & " Started!") Next End Sub Public Sub Test(CurrentInstance As MyThreading) Debug.WriteLine("Results (in order) came back to mainthread from:" & CurrentInstance.MyThreadToStart.Name) End Sub End Class Public Class MyThreading Public DataToPass As String Public CallingThreadForm As Form Public InvokeFunctionOnCompelte As [Delegate] Public MyThreadToStart As Threading.Thread Public PreviousThread As System.Threading.Thread Public Sub New(ByVal NameOfThread As String, ByVal pDataToPass As String, ByVal pPreviousThread As Threading.Thread, ByVal pCallingThreadForm As Form, ByVal pInvokeFunctionOnCompelte As [Delegate]) DataToPass = pDataToPass PreviousThread = pPreviousThread CallingThreadForm = pCallingThreadForm InvokeFunctionOnCompelte = pInvokeFunctionOnCompelte MyThreadToStart = New Threading.Thread(AddressOf StartWork) MyThreadToStart.Name = NameOfThread MyThreadToStart.Start() End Sub Private Sub StartWork() If PreviousThread IsNot Nothing Then While PreviousThread.IsAlive 'Debug.WriteLine(MyThreadToStart.Name & ": Sleeping for " & DataToPass & " Seconds") Threading.Thread.Sleep(1000 * CInt(DataToPass)) End While Else Debug.WriteLine(MyThreadToStart.Name & "Previous Thread is nothing") End If Debug.WriteLine(MyThreadToStart.Name & ": Done!") CallingThreadForm.Invoke(InvokeFunctionOnCompelte, Me) End Sub End Class |