Results 1 to 8 of 8

Thread: Multithreading

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    5

    Multithreading

    Not quite certain how to implement multi-threading. I get a Debugger.Runtime.CrossThreadingMessagingException even if I invoked "Control.CheckForIllegalCrossThreadCalls = False" within the body of code. The code reacts to an unplugging of a USB dongle event, and I want to update a combobox SelectedIndex, but this is where the error results. The code below. Any direction on how to handle this would be greatly appreciated.

    Code:
    Public Sub USBRemoved_EventArrived(ByVal sender As Object, _
        ByVal e As System.Management.EventArrivedEventArgs) _
        Handles USBRemoved.EventArrived
    
            Dim DeviceID As String
            Dim RmComPort As String
            Dim DeviceIndx As Integer
            Dim Test
    
    
            Control.CheckForIllegalCrossThreadCalls = False    
            
    Try
    
                Dim smo As sm.ManagementBaseObject
                smo = CType(e.NewEvent.Properties("TargetInstance").Value,  _
                      sm.ManagementBaseObject)
    
                'search through _USBDevices for Path retrieved from Dependent
                Dim mp As New sm.ManagementPath(smo("Dependent").ToString)
                Dim path As String = mp.RelativePath
    
                path = path.Substring(path.IndexOf("=") + 2)
                path = path.Substring(0, path.Length - 1)
    
                If ((InStr(path, "VID_0403") <> 0) And (InStr(path, "PID_6001") <> 0)) Then
                    'CloseComPort()
                    Control.CheckForIllegalCrossThreadCalls = False     
                    ParL2 = InStr(path, "PID_6001+") + 9
                    ParR2 = InStr(path, "\\0000")
                    If (ParR2 <> 0) Then
                        DeviceID = Microsoft.VisualBasic.Mid(path, ParL2, ParR2 - ParL2)
                        RmComPort = BTDevice(DeviceID)  'Report Associated ComPort to be Removed.
                        DeviceIndx = DeviceIndex(RmComPort)
                        myComPort.RemoveAt(DeviceIndex(RmComPort))
                        'Determine which index is impacted
                        USB_COMPORT2.Items.Remove(RmComPort)
                        USB_COMPORT2.SelectedIndex = USB_COMPORT2.Items.Count - 1    'This line is giving me the problem
                        DeviceIndex.Remove(BTDevice(DeviceID))
                        BTDevice.Remove(DeviceID)
    
    
                    End If
    
                    'to take off this limitation
                    'USB_COMPORT.Text = " "
    
                    BT_DETACH.Enabled = False
                    DevID.Text = " "
                    'RemoveHandler myComPort.DataReceived, AddressOf DataReceivedHandler
                End If
    
    
                For i As Integer = 0 To _USBDevices.Count - 1
                    With _USBDevices(i)
                        If .Path = mp.Path Then
                            BeginInvoke(USBChanged, New Object() _
                            {"Removed: " & .Info & _
                            If(.ExtraDescription <> "", "; " & .ExtraDescription, "") & vbCrLf})
                            _USBDevices.RemoveAt(i)
                            Exit For
                        End If
                    End With
                Next i
    
    
            Catch ex As Exception
    
                Test = 1
    
            End Try
    
    
    
    
    
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multithreading

    Never set CheckForIllegalCrossThreadCalls to False. Follow the CodeBank link in my signature and check out my thread on Accessing Controls From Worker Threads to learn the proper way update the UI based on results from a secondary thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    5

    Re: Multithreading

    OK,

    I created the following:

    Private Sub USB_COMPORT2_IndexSelect()

    If Me.USB_COMPORT2.InvokeRequired Then
    Me.USB_COMPORT2.Invoke(New MethodInvoker(AddressOf USB_COMPORT2_IndexSelect))
    Else
    USB_COMPORT2.SelectedIndex = USB_COMPORT2.Items.Count - 1
    End If

    End Sub

    And While I am not getting an error, behavior is still not right. First, under the Watch Window, USB_COMPORT2.SelectedIndex is grayed out, as if it is not even active.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Multithreading

    What happened to ... USB_COMPORT2.Items.Remove(RmComPort) ?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    5

    Re: Multithreading

    I added it but now I generate a new error since I am passing a value, and it doesn't look like the term after AddressOf can contain an argument.

    Private Sub USB_COMPORT2_IndexSelect(ByRef RmComPort As String)

    If Me.USB_COMPORT2.InvokeRequired Then
    Me.USB_COMPORT2.BeginInvoke(New MethodInvoker(AddressOf USB_COMPORT2_IndexSelect))
    Else
    USB_COMPORT2.SelectedIndex = USB_COMPORT2.Items.Count - 1
    USB_COMPORT2.Items.Remove(RmComPort)
    End If

    End Sub

    'generates an error

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    5

    Re: Multithreading

    Here's another one I can get to work with this construct. Trying to update a Label. ComPort in this example is a global Variable.
    Do I need to use these in conjunction with a background worker?


    Private Sub DevID_Update()

    If Me.DevID.InvokeRequired Then
    Me.DevID.Invoke(New MethodInvoker(AddressOf DevID_Update))
    Else
    DevID.Text = DeviceAddress(ComPort)
    End If


    End Sub

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Multithreading

    Try it this way ...

    vb.net Code:
    1. Delegate Sub IndexSelectCallback(RmComPort As String) ' form scope
    2.  
    3.     Private Sub USB_COMPORT2_IndexSelect(ByVal RmComPort As String)
    4.         If Me.USB_COMPORT2.InvokeRequired Then
    5.             Dim cb As New IndexSelectCallback(AddressOf USB_COMPORT2_IndexSelect)
    6.             Me.USB_COMPORT2.Invoke(cb, New Object() {RmComPort})
    7.         Else
    8.             Me.USB_COMPORT2.SelectedIndex = USB_COMPORT2.Items.Count - 1
    9.             Me.USB_COMPORT2.Items.Remove(RmComPort)
    10.         End If
    11.     End Sub
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    5

    Re: Multithreading

    It works! Thanks a bunch!

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