Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

Threading Problem with labels created at runtime

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
0
JPERKS1985
Asked:
JPERKS1985
  • 4
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
JPERKS1985Author 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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Gotcha...

It seems to me that you are NOT creating a NEW label when you are creating your new thread:

    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

Here you are passing in the same "WorkerEventLabel" to all of the threads created!

You need to declare a local Label var and pass that in:

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim thisIsANewLabel As New Label
        thisIsANewLabel.Size = New System.Drawing.Size(285, 15)
        thisIsANewLabel.Location = New System.Drawing.Point(WorkerInDivPanel.Left + 10, WorkerInDivPanel.Top + 15)
        thisIsANewLabel.Name = "WorkerEventLabel" & WorkerCount
        thisIsANewLabel.Visible = True
        thisIsANewLabel.Text = "No Event Found"

        ' don't forget to add the new label to your form
        ' if you dont' want it added to the root of the form then replace "Me" with the desired container (like Panel1)
        Me.Controls.Add(thisIsANewLabel)        

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

*** BUT *** you obviously need to position each new Label differently than you are now...otherwise they will all be on top of each other.  =)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JPERKS1985Author 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
0
 
JPERKS1985Author Commented:
I got it working thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So what was the problem?  I've been starting at the code and can't see anything obviously wrong...
0
 
JPERKS1985Author 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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now