Solved

Threading Problem with labels created at runtime

Posted on 2006-06-14
7
423 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
0
Comment
Question by:JPERKS1985
  • 4
  • 3
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16906846
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
 
LVL 1

Author Comment

by:JPERKS1985
ID: 16906976
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 16907032
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:JPERKS1985
ID: 16907151
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
 
LVL 1

Author Comment

by:JPERKS1985
ID: 16907273
I got it working thanks
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16907286
So what was the problem?  I've been starting at the code and can't see anything obviously wrong...
0
 
LVL 1

Author Comment

by:JPERKS1985
ID: 16916918
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countAbc challenge 9 49
stringclean challenge 26 56
word0 challenge 3 58
Windows Service to Receive TCP Packets 4 43
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now