Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[Basic] [Tutorial] ★Counter Using Multi-Threading ★


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

Let'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 = 0

Now 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 Sub

Now 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 Sub

Now, 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 Sub

Now 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.
Great tutorial and explanation, thank you for contributing.
(Apr 25, 2011 05:26 PM)Deceptive Wrote:  Great tutorial and explanation, thank you for contributing.

Thank you, and you're quite welcome.
I created this because I thought it might help other members, because Multi-Threading sure as hell helped me xD
Nice job. I have a different method of doing it, but yours is nice too.
Awesome tutorial, I like how you didn't use "CheckForIllegalCrossThreadCalls = False."
genious is called crossThread more Explain here
http://www.codeproject.com/KB/cs/Cross_Thread.aspx
Might want to add abort, for form closing.
Please explain the last two snippets (settext sub & button click)
I want to learn but don't understand what's going on :L
This will be very helpful for a project I am working on.
Thanks
Perplexity, i'm trying to make your example universal for my program...

Code
Public Sub SetText(ByVal ctl As Control, ByVal Text As String)
  If ctl.InvokeRequired Then
    Dim a As New ContextCallback(AddressOf SetText)
    frmMain.Invoke(a, New Object() {Text})
  Else
    ctl.Text = Text
  End If
    End Sub

but i'm getting an error on ContextCallback(AddressOf SetText)

am i missing something here?
(Aug 17, 2011 06:10 PM)seifertson Wrote:  Perplexity, i'm trying to make your example universal for my program...

Code
Public Sub SetText(ByVal ctl As Control, ByVal Text As String)
  If ctl.InvokeRequired Then
    Dim a As New ContextCallback(AddressOf SetText)
    frmMain.Invoke(a, New Object() {Text})
  Else
    ctl.Text = Text
  End If
    End Sub

but i'm getting an error on ContextCallback(AddressOf SetText)

am i missing something here?

Please provide the error you're getting and maybe I can help you out.
sure, the error i get is...
Code
Method 'Public Sub SetText(ctl As System.Windows.Forms.Control, Text As String)' does not have a signature compatible with delegate 'Delegate Sub ContextCallback(state As Object)'.
Thanks for the code! I was looking for this for a long time. You helped me 👍
https://github.com/Washi1337

"I'll tell you this UDP joke, and I don't care if you will get it or not."
(Aug 17, 2011 09:26 PM)seifertson Wrote:  sure, the error i get is...
Code
Method 'Public Sub SetText(ctl As System.Windows.Forms.Control, Text As String)' does not have a signature compatible with delegate 'Delegate Sub ContextCallback(state As Object)'.

Re-read your code, you obviously did something wrong :3

(Aug 17, 2011 11:12 PM)Washi Wrote:  Thanks for the code! I was looking for this for a long time. You helped me 👍

I'm glad this helped you, pm me if you have any questions :)
Finally, I have been wondering about this idea for a while now, and actually needed to implement this into my new project, luckily I stumbled upon this. Thanks soo much, I am greatfull. Keep it up!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by mrKOMay 29, 2020 09:50 AM 4 232 May 29, 2020 09:50 AM
Last Post: CD_ROM
[Python] by -DarkByDesign-May 15, 2020 02:45 AM 7 180 May 15, 2020 02:45 AM
Last Post: Athr
[Java] by -DarkByDesign-May 14, 2020 12:13 PM 0 129 May 14, 2020 12:13 PM
Last Post: -DarkByDesign-