Newbie Learning Threading
- From: Bishop <Bishop@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 26 May 2005 09:23:17 -0700
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
.
- Follow-Ups:
- Re: Newbie Learning Threading
- From: Cor Ligthert
- Re: Newbie Learning Threading
- From: Jon Skeet [C# MVP]
- Re: Newbie Learning Threading
- Prev by Date: How to use Crystal Reports in asp.net web application?
- Next by Date: Re: Oracle client and .net
- Previous by thread: How to use Crystal Reports in asp.net web application?
- Next by thread: Re: Newbie Learning Threading
- Index(es):
Relevant Pages
|