VB.Net – Generating Multiple Thread's at once but Get the Results in ORDER!

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.

Leave a comment

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