Newbie Learning Threading

Tech-Archive recommends: Speed Up your PC by fixing your registry



I have a class which I create 6 instances of. Each class has unique numbers
passed when they are created. As they are running they throw an event that
populates a listbox. the data in the list box starts out fine 0,2,4,6,8,10
but instead of repeating, it just says 10,10,10,10....

I'm trying to understand why this is happening (I'm guessing either the last
thread is the only one running or the variables in each of the other threads
are overwritten with the values used when the last one is created) and how I
should be doing this.

My goal is to have 6 threads taking they're turn adding numbers to the list
box. i.e.
0,2,4,6,8,10,0,2,4,6,8,10,0,2,4,6,8,10,0,2,4,6,8,10,0,2,4,6,8,10.....

Below is the code I'm currently using:

Imports System.Threading

Public Class TestThreading
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
<Content Removed for Readibility>
#End Region

Dim t As Thread
Dim WithEvents myClsThread As clsThread

Sub myThreadUpdate(ByVal myAnswer As Integer) Handles
myClsThread.myThreadUpdate
ListBox1.Items.Add(myAnswer)
End Sub

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
Dim x As Integer
For x = 0 To 5
myClsThread = New clsThread
myClsThread.num1 = x
myClsThread.num2 = x


t = New Thread(AddressOf myClsThread.ADD)
t.Start()
System.Threading.Thread.Sleep(1000)
Next
End Sub
End Class

Public Class clsThread
Public num1 As Integer
Public num2 As Integer
Public Answer As Integer
Public Event myThreadUpdate(ByVal answer As Integer)

Public Sub ADD()
Answer = num1 + num2
Dim myStop As Boolean = False
Do Until myStop = True
RaiseEvent myThreadUpdate(Answer)
System.Threading.Thread.Sleep(1000)
Loop
End Sub
End Class
.



Relevant Pages

  • Force processor to equally spend time between clients ?
    ... Say there are 3 client ... Private Sub objWinsock_wDataArrival(index As Integer, ... Public Event wConnect ...
    (microsoft.public.vb.general.discussion)
  • Re: How to create what VB6 calls an ActiveX EXE
    ... public event workcomplete ... public sub dowork() ... you use that, as SQL Server has a great level of control over the CLR, ... I agree on all accounts but the COM dll is unmananged code. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using RaiseEvents
    ... Publice Event Sub ConnectionDrop ... when they drop the non-ui control to the application form. ... Public Event OnPageMessageHandler ... that your events have a parameter "Sender" As Object and a parameter ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Force processor to equally spend time between clients ?
    ... Which end is the "client"? ... > Private Sub objWinsock_wDataArrival(index As Integer, ... > Public Event wConnect ...
    (microsoft.public.vb.general.discussion)
  • API parameter is events Structure
    ... Public Event fingerTouching() ... sub main ... AddHandler evnt.fingerTouching, AddressOf t2 ...
    (microsoft.public.dotnet.languages.vb)