Apr 25, 2011 05:23 PM
(This post was last modified: Apr 25, 2011 05:25 PM by Perplexity.)
★Counter Using Multi-Threading ★
I decided to write this tutorial since I've recently implemented Multi-Threading in my VB.NET RPG Game
First things first
You're going to need to import System.Threading, this isn't exactly needed but it makes life easier.
So declare this above everything:
Code
Imports System.ThreadingLet's get started
Firstly, you're going to need to declare a global integer, so underneath your Form1 Class, put this:
Code
Dim Count As Integer = 0Now then, we're going to need to create a sub for our thread, so make a new sub like this:
Code
Sub AddCount()
Do While True
Count += 1
SetText(Count)
Loop
End SubNow what this will do, is keep counting up until the thread is stopped.
Now if you wanted it to only count up to a specific number, you would use something like:
Code
Sub AddCount()
Do While Count < 10000
Count += 1
SetText(Count)
Loop
End SubNow, we're going to need to declare that SetText sub, so use this:
Code
Private Sub SetText(ByVal Text As String)
If Me.Label1.InvokeRequired Then
Dim a As New ContextCallback(AddressOf SetText)
Me.Invoke(a, New Object() {Text})
Else
Me.Label1.Text = Text
End If
End SubNow we need to make it all work, so create a label in your form, leave it named as Label1, this will display the current count.
Also, add a button, leave that named as Button1.
Double click your button and use this code:
Code
Dim countThread As New Thread(New ThreadStart(AddressOf Me.AddCount))
countThread.Start()What this does, is it declares the thread and links it to the sub we created just now.
Now once you click your Button, it should keep counting up until wherever you specified it to stop.




