Hack Forums
[.NET] [Basic] [Tutorial] ★Counter Using Multi-Threading ★ - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [Basic] [Tutorial] ★Counter Using Multi-Threading ★ (/showthread.php?tid=1244340)



[Basic] [Tutorial] ★Counter Using Multi-Threading ★ - Perplexity - Apr 25, 2011


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


* - Deceptive - Apr 25, 2011

Great tutorial and explanation, thank you for contributing.


* - Perplexity - Apr 25, 2011

(Apr 25, 2011 05:26 PM)TheMist 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


* - Marketplace - Apr 25, 2011

Nice job. I have a different method of doing it, but yours is nice too.


* - Cody8295 - Apr 25, 2011

Awesome tutorial, I like how you didn't use "CheckForIllegalCrossThreadCalls = False."


* - DragQueen - Apr 25, 2011

genious is called crossThread more Explain here
http://www.codeproject.com/KB/cs/Cross_Thread.aspx


* - MalwareMop - Apr 25, 2011

Might want to add abort, for form closing.


* - error32 - Apr 26, 2011

Please explain the last two snippets (settext sub & button click)
I want to learn but don't understand what's going on :L


* - Ghost‎ - Apr 26, 2011

This will be very helpful for a project I am working on.
Thanks


* - seifertson - Aug 17, 2011

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?


* - Perplexity - Aug 17, 2011

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


* - seifertson - Aug 17, 2011

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



* - Washi - Aug 17, 2011

Thanks for the code! I was looking for this for a long time. You helped me 👍


* - Perplexity - Aug 17, 2011

(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)TheUnknownProgrammer 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 :)


* - g1ll1s - Aug 17, 2011

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!