none
Sending Multiple Variables To A Helper Worker Thread

    General discussion

  •  So far with a little help I have a worker thread that returns the checkstate of a check box to my form when a port is shorted in my parallel port, this works fine, so i copied steps one and two and changed the variable name to "SingleMotion".  The problem I'm having is how do I pass the variable information to the worker thread originally I had this as a function, and i understand how to pass variables to a function.

    How do i pass variables to my worker thread


    the other problem i have is that currently when i scroll the mouse wheel the "e.delta" change is passed to my form with a worker thread "ByVal e As System.ComponentModel.DoWorkEventArgs" is declared so i have a problem.  I changed the "e" in this statement for another letter i.e "f" is this valid?

    Madaxe



    '(1)Declared within the class
    Private
    WithEvents SingleMotion As System.ComponentModel.BackgroundWorker


    '(2)Start Worker Thread
    SingleMotion = New System.ComponentModel.BackgroundWorker
    SingleMotion.WorkerReportsProgress =
    True
    SingleMotion.WorkerSupportsCancellation = True
    SingleMotion.RunWorkerAsync()


    '(3)Worker Thread
    Public
    Sub SingleMotion_DoWork _
    (
    ByVal e As System.Windows.Forms.MouseEventArgs, ByVal Ai As Integer, ByVal Bi As Integer, ByVal Ci As Integer, _
    ByVal Di As Integer, ByVal Ei As Integer,ByVal Fi As Integer, ByVal Gi As Integer, ByVal Hi As Integer, _
    ByVal PortID As Integer, _ByVal directionStatusStripLabel As Label, ByVal decimalStatusStripLabel As Label, _
    ByVal binaryStatusStripLabel As Label, ByVal Increment As Integer, _ByVal sender As Object, _
    ByVal f As System.ComponentModel.DoWorkEventArgs) Handles SingleMotion.DoWork

    Dim MyIndex As Integer = 1

    For MyIndex = 1 To Increment

        System.Threading.Thread.Sleep(1000)

        If
    (e.Delta > 0) Then

            SingleStepForward(Ai, Bi, Ci, Di, Ei, Fi, Gi, Hi, PortID,directionStatusStripLabel,decimalStatusStripLabel,
                binaryStatusStripLabel)

        ElseIf (e.Delta < 0) Then

            SingleStepReverse(Ai, Bi, Ci, Di, Ei, Fi, Gi, Hi, PortID,directionStatusStripLabel,decimalStatusStripLabel,     
                binaryStatusStripLabel)

        ElseIf (e.Delta = 0) Then

            If (Increment > 0) Then

                SingleStepForward(Ai, Bi, Ci, Di, Ei, Fi, Gi, Hi,portID, directionStatusStripLabel,decimalStatusStripLabel,
                    binaryStatusStripLabel)

            ElseIf (Increment < 0) Then

                SingleStepReverse(Ai, Bi, Ci, Di, Ei, Fi, Gi, Hi, PortID, directionStatusStripLabel, decimalStatusStripLabel,
                    binaryStatusStripLabel)

            End
    If
        End If
    Next
    End Sub


    As Busy As A Bricky In Beirut
    • Edited by Madaxe Thursday, December 04, 2008 12:05 AM dyslexic spelling
    Wednesday, December 03, 2008 11:53 PM

All replies

  •  This is a simpler example how do i send the variable "MyMessage" to the worker thread this example does not work


    Madaxe

    Public Class Form1

    '(1)Declared within the class

    Private WithEvents SingleMotion As System.ComponentModel.BackgroundWorker

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    '(2)Start Worker Thread

    SingleMotion = New System.ComponentModel.BackgroundWorker

    SingleMotion.WorkerReportsProgress = True

    SingleMotion.WorkerSupportsCancellation = True

    SingleMotion.RunWorkerAsync("Luke I Am Your Father")

    End Sub

    '(3)Worker Thread

    Public Sub SingleMotion_DoWork(ByVal MyMessage As String, ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles SingleMotion.DoWork

    Dim Index As Integer = 1

    For Index = 1 To 10

    MsgBox(Index & ")" & MyMessage)

    Next

    End Sub

    End Class


    As Busy As A Bricky In Beirut
    Thursday, December 04, 2008 12:19 AM
  •  I figured i could declare a public variable set its values anywhere in my script then use those values in my worker thread is this the best way to do this


    Madaxe

    Public
    Class Form1

    Public Myarray(2)

    '(1)Declared within the class

    Private WithEvents SingleMotion As System.ComponentModel.BackgroundWorker

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    '(2)Start Worker Thread

    Myarray(0) = "marcs"
    Myarray(1) = "peter"
    Myarray(2) = "john"

    SingleMotion = New System.ComponentModel.BackgroundWorker
    SingleMotion.WorkerReportsProgress =
    True
    SingleMotion.WorkerSupportsCancellation = True
    SingleMotion.RunWorkerAsync()

    End Sub

    '(3)Worker Thread

    Public Sub SingleMotion_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles SingleMotion.DoWork

    Dim Index As Integer = 1

    For Index = 0 To 2

    MsgBox(Index & Myarray(Index))

    Next

    End Sub

    End Class


    As Busy As A Bricky In Beirut
    Thursday, December 04, 2008 12:41 AM
  • When accesing variables from 2 different threads you can sometimes get into alot of trouble. What if thread1 is trying to read the data at the same time thread2 is updating it? Or if both threads are adding a set amount based on current value and both threads gets to read the value before setting the value. This is called a Race Condition.

    When working with the BackgroundWorker you have the ability to send and object to it as an argument. This argument can then be read and used ind your BGWorker and upon end be returned back.

    Take a look at this code. It demonstrates the use of arguments and return values. Argument and e.Result takes an Object so you can put whatever into it. I usually create some sort or return object with a Status and a Data field or something like that.


    Public Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            BackgroundWorker1.RunWorkerAsync("This is the passthrough object")  
        End Sub 
     
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.ObjectByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork  
            Dim passThroughObject As String = CType(e.Argument, String)  
            MessageBox.Show("Message from worker thread: " & passThroughObject)  
            passThroughObject = "We now return this" 
            e.Result = passThroughObject  
        End Sub 
     
        Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.ObjectByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted  
            Dim result As String = CType(e.Result, String)  
            MessageBox.Show(result)  
        End Sub 
     
    End Class 
    Thursday, December 04, 2008 8:37 AM