Complete Guide To Multi Threading
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
10 posts
Page 1 of 1
Hey guys, this would be my first tutorial up here and its going to be about multi threading. Working with multiple threads can be quite difficult, it always comes up new errors and when you fixed those it just comes more of them.
So, if your not that advanced in programming, it can be hard to catch up, but you can at least try!
I included an example at the bottom of the page so you can test multi threading.
This tutorial includes:
Here is an example and some source files for you to study
So, if your not that advanced in programming, it can be hard to catch up, but you can at least try!
I included an example at the bottom of the page so you can test multi threading.
This tutorial includes:
- Description about threads and delegates
Working with threads
Working with delegates
Accessing a User Interface(UI) from another thread
- A thread is used to perform several operations at the same time, for example:
You make a conversion program, it converts between images and icons. If the file is big, the program will freeze when you start converting. To prevent this, do the conversion in another thread. This way the program continues normal without freezing, and the conversion will be finished faster.
- To make a new thread, import the namespace System.Threading at the top of the code file.
Then in a method paste this piece of code:Code: Select allReplace 'Procedure' with the name of the method(a 'Sub' or 'Function').Dim t As New Thread(New ThreadStart(AddressOf Procedure))
Note that we didn't only create a new Thread, but we also created a 'ThreadStart'. This is a delegate used for making a new thread. The 'ThreadStart' is a delegate without any parameter, witch means you can't pass any arguments, but if you have a method witch needs an argument you need to change ThreadStart with ParameterizedThreadStart, this method can pass one argument. If you still need more, you have to change your methods arguments into an array, then split up the arguments inside the method.
Starting the thread is pretty simple, you just write this line of code inCode: Select allThe Start() method is overloaded once, the overloaded method can have one parameter, witch needs the thread to be created with a ParameterizedThreadStart. This parameter will be passed to your method.t.Start()
*Notice that 't' MUST be changed into he name of your thread, else you will get an error.*
- A delegate is an indirect way of accessing a method. The 'ThreadStart' and 'ParameterizedThreadStart' we talked about earlier is delegates. The delegate class is marked with MustInherit' so you can't just use the New keyword to make one. Instead you have to paste this line of code outside a method, but inside a class. Code: Select all*Note that you can change 'Sub' to 'Function' if you want it to be a function.*
Public Delegate Sub MyDelegate()
*Also note that you can change the access level of the delegate.*
Inside the parentheses you are allowed to have parameters or arguments, these can be created by using the ByVal or ByRef statements. And of course are you free to change the name of the delegate :lol:
To create a new instance of the delegate you created, paste this code inside a methodCode: Select allChange 'aMethod' to the name of a method that have a compatible 'Argument Signature' as the delegate.Dim Deleg As New MyDelegate(AddressOf aMethod)
- A UI can be defined as a form or control. When accessing a UI from another thread without the right code will throw a InvalidOperationException. To prevent this, you have to use delegates and the Control.Invoke() method.
Another tutorial with more examples can be found at msnd.microsoft.com, here.
To access a Control from a different thread than it was created on is proven to throw a lot of errors. So I recommend to read the MSDN tutorial i talked about.
Get to the point stupid :x
Okay so when the program calls from another thread, it may create an exception. To prevent this we use delegates and the Control.Invoke or Control.BeginInvoke method. Here is a sample for setting the text in a textbox from a separate thread.Code: Select allIn this piece of code, we defined a delegate named SetTextDelegate. This delegate had two parameters, this was because it has to fit with the methods parameters. But what happens in the code? Well, First it checks if the TextBox needs to invoke before it can be accessed, if it has to, it raises the invoke method with a new SetTextDelegate witch is made by the AddressOf operator, the delegates 'Address' is the SetText method. This method has two parameters, so we pass those in a param array. So after the textbox is invoked, we can access it. When we are on the same thread as the textbox was created on the InvokeRequired property is set to false, so then the code that sets the text runs. This way it comes up no errors wahooo;Delegate Sub SetTextDelegate(ByVal Txt As Sting, ByVal Box As System.Windows.Forms.TextBox) Sub SetText(ByVal Text As String, ByVal TextBox As System.Windows.Forms.TextBox) If TextBox.InvokeRequired Then TextBox.Invoke(New SetTextDelegate(AddressOf SetText), {Text, TextBox}) Else TextBox.Text = Text End If End Sub
Here is an example and some source files for you to study

You do not have the required permissions to view the files attached to this post.
Last edited by MrAksel on Mon Dec 19, 2011 9:02 pm, edited 2 times in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

Guys, is there something i should improve, or dont you want to read this?
Sorry for the bump, but i spent alot of time making this tut
Sorry for the bump, but i spent alot of time making this tut
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

Congratulations MrAksel, you're tutorial is the tutorial of the month!
Awesome tutorial man!! Also congrats your tut is the tut of the month. cooll;
Instead of LOL use this -
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
Thanks for de deciding that this was the tut of the month, I really appreciate that.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

Congrats dude ur tut won tutorial of the month cooll;
Great tutorial! Was the sample project file coded in Visual Basic 2010? The .exe file in Release folder requires .NET Framework 4.0.
Yeah, sorry Nexon. But you can open the files right?
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

MrAksel wrote:Yeah, sorry Nexon. But you can open the files right?Yes i can open the files, just upgraded to VB2010. After reading your thread twice i still don't really understand how it works ugh, gotta spend more time on it.
Last bumped by MrAksel on Thu Jun 07, 2012 2:23 pm.
10 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023