Results 1 to 8 of 8

Thread: How to get a variable/value out of a thread?

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2010
    Posts
    93

    How to get a variable/value out of a thread?

    I want to write some code to do some processing. This seems best done in a thread. But I also want some UI and general controlling passed to and from it. eg: Adding lines to a text box for general UI updating while it's running.

    Now, let's assume I might have multiple threads running, so each thread can call my UpdateUI subroutine. But the problem is, at the moment my code is using a public variable UI_Msg to pass values back from each thread to the UpdateUI subroutine. This hits me as dangerous as different threads may overwrite UI_Msg at the same time.

    Instead I tryied to attach a class to each thread, so each thread has its own UI_Msg property/value. When I then get to UpdateUI, I want to pluck this value out of that thread, so there's no risk of different threads overwriting each others msgs to UpdateUI.


    I've attached my code example.

    So all I want to do is in UpdateUI, be able to look at UI_msg from the class for the thread calling UpdateUI. eg: Obtain thread.UI_msg for example...

    Code:
    Imports System.Threading
    
    Public Class MainForm
        Public UI_msg As String
    
        ' Used to track which thread called the UI Update function.
        Private callingThread As Integer
    
        ' The Delegate that is invoked by the control on the form that needs to be updated.
        Delegate Sub UIDelegate()
    
        Private Sub UpdateUI()
            ' If InvokeRequired is true then the call is being made on a thread other
            ' than the UI thread.
            If Label1.InvokeRequired Then
                ' Call UpdateUI by invoking a delegate with the UI control
                callingThread = System.Threading.Thread.CurrentThread.ManagedThreadId()
    
                Dim newDelegate As New UIDelegate(AddressOf UpdateUI)
                Label1.Invoke(newDelegate)
            Else
                Label1.AppendText(Text + "Thread " + callingThread.ToString() + ":" + UI_msg + vbCrLf)
                ' ** How can I get the UI_MSG from the thread class? **
                UI_msg = ""
                Label1.ScrollToCaret()
            End If
        End Sub
    
        Private Sub StartJob_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartJob.Click
            UI_msg = "Starting a new thread"
            UpdateUI()
    
            Dim JobThread As New Thread(AddressOf RunJob)
            JobThread.Start()
        End Sub
    
        Class JobObjClass
            Public JobCanceled As Boolean
            Public UI_Msg As String
        End Class
    
        Sub RunJob()
            Dim StateObj As New JobObjClass()
            StateObj.JobCanceled = False
    
            Dim ctr As Integer
            ctr = 0
            While (ctr < 5 And abort = False)
                System.Threading.Thread.Sleep(1000)
                ctr = ctr + 1
                UI_msg = "Count " & ctr.ToString()
                StateObj.UI_Msg = "Count " & ctr.ToString() ' ** Want to use this instead of previous line!
                UpdateUI()
            End While
    
            StateObj.JobCanceled = True
            UI_msg = "Ending thread"
            UpdateUI()
        End Sub
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            abort = True
        End Sub
    End Class
    I hope this makes sense... I'm only a week or so into Visual Basic.
    Last edited by NeilF; May 28th, 2010 at 10:51 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width