• Log in or sign up for an account here: https://www.sythe.org/login

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

Blupig

BEEF TOILET

$5 USD Donor
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Age
33
Location
Canada
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
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
 
Nice tutorial, but why would you download more code when .Net already support multithreading right out of the box?
 
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
Nice tutorial, but why would you download more code when .Net already support multithreading right out of the box?

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.
 
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.

Ah yeah, I see. I just added multithreading to a program I wrote recently and I forgot how much of a pain Invoking was.
 
Runescape Gamblingrunescape gambling
Back
Top