Solved

Threading Problem with labels created at runtime

Posted on 2006-06-14
7
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 86

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 86

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 86

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression

623 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