Page 1 of 1

Complete Guide To Multi Threading

Posted: Sat Jan 01, 2011 7:34 pm
by MrAksel
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:
  • Description about threads and delegates
    Working with threads
    Working with delegates
    Accessing a User Interface(UI) from another thread
Description about threads and delegates
  • 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.
Working with threads
  • 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 all
    Dim t As New Thread(New ThreadStart(AddressOf Procedure))
    Replace 'Procedure' with the name of the method(a 'Sub' or 'Function').
    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 in
    Code: Select all
    t.Start()
    The 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.
    *Notice that 't' MUST be changed into he name of your thread, else you will get an error.*
Working with delegates
  • 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
    Public Delegate Sub MyDelegate()
    *Note that you can change 'Sub' to 'Function' if you want it to be a function.*
    *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 method
    Code: Select all
    Dim Deleg As New MyDelegate(AddressOf aMethod)
    Change 'aMethod' to the name of a method that have a compatible 'Argument Signature' as the delegate.
Accessing a User Interface(UI) from another thread
  • 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 all
     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
    In 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;
So, if you didn't understand this please post a comment. If anybody have some suggestions, feel free post them ;)

Here is an example and some source files for you to study :)

Re: Complete Guide To Multi Threading

Posted: Tue Jan 04, 2011 9:58 pm
by code it
Thanks!
This is cooll :P

Re: Complete Guide To Multi Threading

Posted: Sun Jan 23, 2011 12:37 pm
by MrAksel
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

Re: Complete Guide To Multi Threading

Posted: Fri Jan 28, 2011 3:12 am
by Usman55
Congratulations MrAksel, you're tutorial is the tutorial of the month!

Re: Complete Guide To Multi Threading

Posted: Fri Jan 28, 2011 11:37 am
by Skillful
Awesome tutorial man!! Also congrats your tut is the tut of the month. cooll;

Re: Complete Guide To Multi Threading

Posted: Fri Jan 28, 2011 2:14 pm
by MrAksel
Thanks for de deciding that this was the tut of the month, I really appreciate that.

Re: Complete Guide To Multi Threading

Posted: Thu Feb 03, 2011 5:08 pm
by RishiRox
Congrats dude ur tut won tutorial of the month cooll;

Re: Complete Guide To Multi Threading

Posted: Sat Feb 05, 2011 3:34 am
by Nexon
Great tutorial! Was the sample project file coded in Visual Basic 2010? The .exe file in Release folder requires .NET Framework 4.0.

Re: Complete Guide To Multi Threading

Posted: Sat Feb 05, 2011 4:51 pm
by MrAksel
Yeah, sorry Nexon. But you can open the files right?

Re: Complete Guide To Multi Threading

Posted: Thu Jun 07, 2012 2:23 pm
by Nexon
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.