[TUT][VB.NET] Multithreading made painfully easy, in 4 steps

Discussion in 'Programming General' started by Blupig, May 30, 2012.

[TUT][VB.NET] Multithreading made painfully easy, in 4 steps
  1. Unread #1 - May 30, 2012 at 6:57 PM
  2. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [TUT][VB.NET] Multithreading made painfully easy, in 4 steps

    Hey guys, in this tutorial I'm going to show you how to do really really easy multithreading with VB.NET using the Async CTP.

    What you'll need:
    - Visual Basic .NET 2010 (Express is fine, but any distro of the suite works)
    - Visual Studio SP1 (if you don't have it already, link here: http://www.microsoft.com/en-us/download/details.aspx?id=23691) INSTALL THIS BEFORE YOU INSTALL THE ASYNC CTP!
    - Visual Studio Async CTP (link here: http://www.microsoft.com/en-us/download/details.aspx?id=9983)

    What is multithreading?
    If you don't know what it is, then you don't need it. Shoo!

    Step 1
    Make a function as you would normally. For this purpose, we're just going to download the Runescape title page and return it as a string.

    Code:
    Private Function GetPage() As String
    	
    	Dim Client As New System.Net.Webclient
    	Dim HTML As String = Client.DownloadString("http://www.runescape.com/")
    	Return HTML
    
    End Function
    
    Step 2
    Make a GUI that displays this text. A simple form with a label or a textbox on it will do fine.

    Code:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
    	TextBox1.Multiline = True
    	TextBox1.Size = New Size(200, 200)
    	TextBox1.Text = GetPage()
    
    End Sub
    So now what we have is a GUI that downloads the Runescape home page and displays its HTML. That's great and all, but did you notice that while the page is downloading, the GUI freezes? That's no good. We want our form to load while the page is being downloaded so that it won't freeze up. In comes the multithreading!

    Step 3
    Go back to your GetPage() function, and make its first line look like this:

    Code:
    Private Async Function GetPage() As Task(Of String)
    
    Here what we're doing is making an asynchronous function. Instead of returning a string, async functions return tasks. Basically, we're going to return an action that returns a string as opposed to returning the string itself.

    Step 4
    Now, we need to add an "Await" statement. This statement pumps a value from a Task into a variable once the task is complete. We can then return the variable, and get what we want out of the function.

    Go back to your GetPage() function, and add the following line underneath the first line we edited in the last step:

    Code:
    Dim GetPageTask As Task(Of String) = Task.Factory.StartNew(Function()
    
    No, I did not forget to finish that line. This is exactly how it has to look. Now, to fix this, we add another small line at the very end of our function right above the "End Function" line:

    Code:
    End Function)
    
    That's probably a bit confusing right? Right now, we've got a function within a function. Crazy! Here's what your function should look like so far:
    Code:
        Private Async Function GetPage() As Task(Of String)
    
            Dim GetPageTask As Task(Of String) = Task.Factory.StartNew(Function()
    
                                                                           Dim Client As New System.Net.WebClient
                                                                           Dim HTML As String = Client.DownloadString("http://www.runescape.com/")
                                                                           Return HTML
    
                                                                       End Function)
    
        End Function
    Now, our function doesn't actually technically return anything anymore, since the meat of it is inside a task that we haven't managed yet. To fix that, we need to wait for our GetPageTask to be finished with its instruction, then return the value. Add this underneath your "End Function)" line, right above the "End Function" line that finished your function for good:

    Code:
    Dim result as String = Await GetPageTask
    Return result
    
    So now, your entire function code should look like this:

    Code:
        Private Async Function GetPage() As Task(Of String)
    
            Dim GetPageTask As Task(Of String) = Task.Factory.StartNew(Function()
    
                                                                           Dim Client As New System.Net.WebClient
                                                                           Dim HTML As String = Client.DownloadString("http://www.runescape.com/")
                                                                           Return HTML
    
                                                                       End Function)
            Dim result As String = Await GetPageTask
            Return result
    
        End Function
    Easy right?

    Now, go back to the form where you displayed your information. We need to make a new task that will store the task returned from our function, then we'll make an await statement to return the value we want.

    Code:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
    	TextBox1.Multiline = True
    	TextBox1.Size = New Size(200, 200)
    
    	Dim MyTask As Task(Of String) = GetPage()
    	Dim HTMLPage As String = Await MyTask
    
    	TextBox1.Text = HTMLPage
    	
    
    End Sub
    Hope that helped out. I know that regular multithreading with delegates and dispatchers can get pretty messy and confusing, so this is really a nice alternative isn't it?

    Tutorial by Blupig
     
  3. Unread #2 - May 31, 2012 at 10:59 PM
  4. Kyle_Undefined
    Joined:
    Feb 9, 2012
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0

    Kyle_Undefined Member

    [TUT][VB.NET] Multithreading made painfully easy, in 4 steps

    Nice tutorial, but why would you download more code when .Net already support multithreading right out of the box?
     
  5. Unread #3 - Jun 5, 2012 at 3:52 PM
  6. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [TUT][VB.NET] Multithreading made painfully easy, in 4 steps

    To avoid using delegates and invoking. Using the async CTP is very clean, easy to use and understand, and is good at what it does. Plus it's good practice for using the types of multithreading statements that are being shipped with .NET 4.5, since they're fairly similar.
     
  7. Unread #4 - Jun 9, 2012 at 9:37 PM
  8. Kyle_Undefined
    Joined:
    Feb 9, 2012
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0

    Kyle_Undefined Member

    [TUT][VB.NET] Multithreading made painfully easy, in 4 steps

    Ah yeah, I see. I just added multithreading to a program I wrote recently and I forgot how much of a pain Invoking was.
     
< [HELP] How to make a RS Loader in VB.Net | [VB.NET][TUT] Creating And Implementing HTTP Handlers >

Users viewing this thread
1 guest


 
 
Adblock breaks this site