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
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Complete Guide To Multi Threading
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 :)
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
User avatar
code it
VIP - Site Partner
VIP - Site Partner
Posts: 821
Joined: Sun Oct 10, 2010 3:02 pm

Thanks!
This is cooll :P
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

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
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
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Congratulations MrAksel, you're tutorial is the tutorial of the month!
Image
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

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!
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

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
User avatar
RishiRox
New Member
New Member
Posts: 16
Joined: Mon Jan 17, 2011 4:05 pm

Congrats dude ur tut won tutorial of the month cooll;
User avatar
Nexon
VIP - Donator
VIP - Donator
Posts: 3
Joined: Sat Dec 18, 2010 2:27 pm

Re: Complete Guide To Multi Threading
Nexon
Great tutorial! Was the sample project file coded in Visual Basic 2010? The .exe file in Release folder requires .NET Framework 4.0.
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

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
User avatar
Nexon
VIP - Donator
VIP - Donator
Posts: 3
Joined: Sat Dec 18, 2010 2:27 pm

Re: Complete Guide To Multi Threading
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.
Last bumped by MrAksel on Thu Jun 07, 2012 2:23 pm.
10 posts Page 1 of 1
Return to “Tutorials”