RaiseEvents causing unsafe cross thread?

G

Guest

Hello to all,
I had to convert a VB classic app to .net and I chose to use 2005.

In the app I have a class that interfaces to several devices. A vision
system, barcode scanner, etc, etc.

When these devices are triggered I respond and once I'm done with the
processing of the logic I raise events from inside the class that update
textboxes and labels on the form.

It's a very simple event. I am just posting text based on conditions. But I
get an unsafe cross thread error when I do this.

'** Forms Event Sample
Private Sub mMachCtl_NotDecoded() Handles mMachCtl.NotDecoded
txtColor.Text = "Error!"
txtLotNum.Text = "Error!"
txtQuantity.Text = "Error!"
txtQuantity.Text = "Error!"
txtWorkOrderId.Text = "Error!"
txtWoNum.Text = "Error!"
BackColor = System.Drawing.Color.Red
lblColorScreen.Text = "COLOR NOT DETECTED!!!"
Dim Ctl As System.Windows.Forms.Control
On Error Resume Next
For Each Ctl In Me.Controls
If Not TypeOf Ctl Is System.Windows.Forms.TextBox Then
Ctl.BackColor = System.Drawing.Color.Red
Next Ctl
End Sub

So I did some research and found out I could do one of two things.

I could use the following(Which I'm not a big fan of)
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf (ctl) Is TextBox Or TypeOf (ctl) Is Label Then
Control.CheckForIllegalCrossThreadCalls = False
End If
Next

or I have to use a delegate and call it like so

Delegate Sub SetTextCallback(ByVal [text] As String)

Private Sub SetText(ByVal [text] As String)
If txtColor.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
End If
End Sub

I'm ok with this method but I can't modify it to be generic. I want to pass
to the SetText sub the control that I want to set the text property on. Yet
when I modify the delegate, the sub and the calls to it the IDE locks up. Is
there a way to modify the delegate, supporting sub and calls to it so that I
can pass something as object?

OR did I miss the boat completely and there is a simpler way to do all of
this?

thanks in advance,
Pat Laf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top