We help IT Professionals succeed at work.

Threading Problem with labels created at runtime

JPERKS1985
JPERKS1985 asked
on
443 Views
Last Modified: 2011-08-18
I think I almost have the code down correctly however as I add a new thread and new labels at runtime the text changes on the new instance of the label and no longer on the first one. Basically I want to assign each thread a different label that is created at runtime to update. Heres the code:


  Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
   
       

WorkerEventLabel.Size = New System.Drawing.Size(285, 15)
        WorkerEventLabel.Location = New System.Drawing.Point(WorkerInDivPanel.Left + 10, WorkerInDivPanel.Top + 15)
        WorkerEventLabel.Name = "WorkerEventLabel" & WorkerCount
        WorkerEventLabel.Visible = True
        WorkerEventLabel.Text = "No Event Found"

   Dim sc As New CWorkerClass
        sc.StartWorkerThread(WorkerEventLabel)
End Sub





Imports System.Threading
Public Class CWorkerClass

    Private WorkerThread As Thread
    Dim MainForm As Form1 = Form1.ActiveForm
    Dim WorkerCount As Integer
    Dim ELabel As Label
 

    Public Sub StartWorkerThread(ByVal EventLabel As Label)
        If WorkerThread Is Nothing Then
            ELabel = EventLabel
            MsgBox(ELabel.Name.ToString)

            WorkerThread = New Thread(AddressOf Me.Worker)
            WorkerThread.Name = "Worker Thread: " & WorkerCount.ToString
            WorkerCount += 1
            WorkerThread.Start()
        End If
    End Sub

    Private Sub Worker()



        Dim RandomClass As New Random
        Dim RandomNumber As Integer
        Dim RCount As Integer
        Dim Agent As String
        Dim EventCode As String
        Dim EventName As String

        Dim CurrentWorker As Integer

        Dim StatusLabel As Label
        Dim endworkers As Integer


   

        StatusLabel = MainForm.GetStatusLabel()
        MsgBox(StatusLabel.Name.ToString)
        Do Until endworkers = 99999999



            RCount = RowCount(MainForm.gridActiveEvent)
            On Error Resume Next
            RandomNumber = RandomClass.Next(0, RCount)


            Agent = MainForm.gridActiveEvent.Item(RandomNumber, 4)
            EventCode = MainForm.gridActiveEvent.Item(RandomNumber, 2)
            EventName = MainForm.gridActiveEvent.Item(RandomNumber, 1)

            Debug.Write("AGENT: " & Agent & " EVENT CODE: " & EventCode & " EVENT NAME: " & EventName & " WORKER COUNT: " & CurrentWorker & vbNewLine)

            ELabel.Text = EventName


            StatusLabel.Text = EventName


            ' EndWorkers += 1
            Debug.Write(endworkers & vbNewLine)


            endworkers += 1
        Loop
        endworkers = 0




    End Sub


End Class
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
You said:

    "Basically I want to assign each thread a different label that is created at runtime to update."

It looks like you've got that covered correctly.
(except that you are incorrectly directly updating the main UI labels from within your thread)


I'm not following this part though:

    "...however as I add a new thread and new labels at runtime the text changes on the new instance of the label and no longer on the first one."

You said each thread is supposed to have its own Label to work with...

So who exactly is supposed to be updating these other labels?

Each thread will exit when its "worker" sub has completed.  If they are running conncurrently then you will get different labels being updated at the same time.

Author

Commented:
Each thread has its own label, when you create a thread a new label is created. The problem is the label all of the created threads updates is the last label generated. I need a way to assign a label to each thread.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I'm sorry I cut the code off a bit.

  Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        EndWorkers = 0 ' Activate Workers
        Dim WorkerEventLabel As New Label
        Dim WorkerStatusLabel As New Label

        Dim PanelHeight As Integer
        Dim WorkerInDivPanel As New GroupBox

        'Add new worker gorup box
        WorkerInDivPanel.Size = New System.Drawing.Size(320, 225)
        WorkerInDivPanel.Location = New System.Drawing.Point(WorkerPanel.Left + 20, workercount * 225 + WorkerPanel.AutoScrollPosition.Y)


        WorkerInDivPanel.Name = "Worker: " & workercount
        WorkerInDivPanel.Text = WorkerInDivPanel.Name
        WorkerInDivPanel.Visible = True



        'Add new label to display workers current event
        WorkerEventLabel.Size = New System.Drawing.Size(285, 15)
        WorkerEventLabel.Location = New System.Drawing.Point(WorkerInDivPanel.Left + 10, WorkerInDivPanel.Top + 15)
        WorkerEventLabel.Name = "WorkerEventLabel" & workercount
        WorkerEventLabel.Visible = True
        WorkerEventLabel.Text = "No Event Found"




        WorkerPanel.Controls.Add(WorkerEventLabel)
      WorkerPanel.Controls.Add(WorkerStatusLabel)
        WorkerPanel.Controls.Add(WorkerInDivPanel)


        'The threading part...
        'Dim WorkerThread As Thread = New Thread(AddressOf Worker)

        '     WorkerThread.Name = "Worker Thread: " & WorkerCount.ToString

        'WorkerThread.IsBackground = True
        'WorkerThread.Start()
        workercount += 1

        WorkerCountLabel.Text = "Worker Count: " & workercount
        Dim sc As New CWorkerClass
        sc.StartWorkerThread(WorkerEventLabel)


    End Sub

Author

Commented:
I got it working thanks
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
So what was the problem?  I've been starting at the code and can't see anything obviously wrong...

Author

Commented:
I had to change the declration scope of the label. Check out my other questions I have a bunch of them. thanks for the help.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.