|
-
May 6th, 2013, 06:24 PM
#1
Thread Starter
New Member
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
-
May 6th, 2013, 07:58 PM
#2
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.
-
May 7th, 2013, 11:21 AM
#3
Thread Starter
New Member
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.
-
May 7th, 2013, 12:18 PM
#4
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!
-
May 7th, 2013, 01:27 PM
#5
Thread Starter
New Member
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
-
May 7th, 2013, 01:34 PM
#6
Thread Starter
New Member
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
-
May 7th, 2013, 01:48 PM
#7
Re: Multithreading
Try it this way ...
vb.net Code:
Delegate Sub IndexSelectCallback(RmComPort As String) ' form scope
Private Sub USB_COMPORT2_IndexSelect(ByVal RmComPort As String)
If Me.USB_COMPORT2.InvokeRequired Then
Dim cb As New IndexSelectCallback(AddressOf USB_COMPORT2_IndexSelect)
Me.USB_COMPORT2.Invoke(cb, New Object() {RmComPort})
Else
Me.USB_COMPORT2.SelectedIndex = USB_COMPORT2.Items.Count - 1
Me.USB_COMPORT2.Items.Remove(RmComPort)
End If
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!
-
May 7th, 2013, 03:22 PM
#8
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|