CheckForIllegalCrossThreadCalls ?

M

Mika M

Help link...

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclictl/html/138f38b6-1099-4fd5-910c-390b41cbad35.htm

....says "You can disable this exception by setting the value of the
CheckForIllegalCrossThreadCalls property to false. This causes your
control to run the same way as it would run under Visual Studio 2003.
"

I'd like to try setting this property to false, but I don't understand
where in the code and how?
 
A

Armin Zingler

Mika M said:
Help link...

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclictl/html/138f38b6-1099-4fd5-910c-390b41cbad35.htm

...says "You can disable this exception by setting the value of the
CheckForIllegalCrossThreadCalls property to false. This causes your
control to run the same way as it would run under Visual Studio
2003. "

I'd like to try setting this property to false, but I don't
understand where in the code and how?


...and why? If you set it to false, you'll get unpredictable results. The
topic you quoted describes this very well.


If you really need to:

Control.CheckForIllegalCrossThreadCalls = False


Armin
 
M

Mika M

Armin said:
..and why? If you set it to false, you'll get unpredictable results.
The topic you quoted describes this very well.

I have made my own class for reading serialport equipment. When
serialport receives data, data is saved into public property like...

Public Event ReaderDataReceived As EventHandler

Private _ReaderData As String = ""

Public Property ReaderData() As String
Get
Return _ReaderData
End Get
Set(ByVal Value As String)
...
_ReaderData = Value
...

RaiseEvent ReaderDataReceived(Me, New EventArgs)
End Set
End Property

Private Sub port_DataReceived(...) Handles port.DataReceived
'// "port" is instance of Framework 2.0 SerialPort
...
Me.ReaderData = port.ReadExisting
...
End Sub


This ReaderData-property is DataBound into Windows Form TextBox like...

txtReaderData.DataBindings.Add("Text", MyClass, "ReaderData")

and it seems not to work without setting
Control.CheckForIllegalCrossThreadCalls = False

Yes I understand it's question about unsafe threads. Handling threads is
something new for me, and this application has both class for handling
serialport and Windows Form for using class as DataBound way. I can't
figure out how to make my applicaton thread safe in this case.
 
H

Herfried K. Wagner [MVP]

Mika M said:
Private Sub port_DataReceived(...) Handles port.DataReceived
'// "port" is instance of Framework 2.0 SerialPort
...
Me.ReaderData = port.ReadExisting
...
End Sub


This ReaderData-property is DataBound into Windows Form TextBox like...

txtReaderData.DataBindings.Add("Text", MyClass, "ReaderData")

and it seems not to work without setting
Control.CheckForIllegalCrossThreadCalls = False

Yes I understand it's question about unsafe threads. Handling threads is
something new for me, and this application has both class for handling
serialport and Windows Form for using class as DataBound way. I can't
figure out how to make my applicaton thread safe in this case.

Instead of setting 'ReaderData' directly from the thread use
'Control.Invoke'/'Control.BeginInvoke' to set it.
 
A

Armin Zingler

Mika M said:
I have made my own class for reading serialport equipment. When
serialport receives data, data is saved into public property like...

Public Event ReaderDataReceived As EventHandler

Private _ReaderData As String = ""

Public Property ReaderData() As String
Get
Return _ReaderData
End Get
Set(ByVal Value As String)
...
_ReaderData = Value
...

RaiseEvent ReaderDataReceived(Me, New EventArgs)
End Set
End Property

Private Sub port_DataReceived(...) Handles port.DataReceived
'// "port" is instance of Framework 2.0 SerialPort
...
Me.ReaderData = port.ReadExisting
...
End Sub


This ReaderData-property is DataBound into Windows Form TextBox
like...

txtReaderData.DataBindings.Add("Text", MyClass, "ReaderData")

and it seems not to work without setting
Control.CheckForIllegalCrossThreadCalls = False

Yes I understand it's question about unsafe threads. Handling
threads is something new for me, and this application has both class
for handling serialport and Windows Form for using class as
DataBound way. I can't figure out how to make my applicaton thread
safe in this case.


Use the control's Invoke/BeginInvoke method to marshal the call to the
thread that created the control.

See also:

Visual Basic Express
Visual Basic Programming Guide
Multithreading in Visual Basic
Multithreading with Forms and Controls


..NET Framework SDK
Windows Applications
Windows Forms
Windows Forms Controls
Developing Custom Windows Forms Controls with _
the .NET Framework
Multithreading in Windows Forms Controls



Armin
 
M

Mika M

Mika said:
I have made my own class for reading serialport equipment. When
serialport receives data, data is saved into public property like...

Public Event ReaderDataReceived As EventHandler

Private _ReaderData As String = ""

Public Property ReaderData() As String
Get
Return _ReaderData
End Get
Set(ByVal Value As String)
...
_ReaderData = Value
...

RaiseEvent ReaderDataReceived(Me, New EventArgs)
End Set
End Property

Private Sub port_DataReceived(...) Handles port.DataReceived
'// "port" is instance of Framework 2.0 SerialPort
...
Me.ReaderData = port.ReadExisting
...
End Sub


This ReaderData-property is DataBound into Windows Form TextBox like...

txtReaderData.DataBindings.Add("Text", MyClass, "ReaderData")

and it seems not to work without setting
Control.CheckForIllegalCrossThreadCalls = False

Yes I understand it's question about unsafe threads. Handling threads is
something new for me, and this application has both class for handling
serialport and Windows Form for using class as DataBound way. I can't
figure out how to make my applicaton thread safe in this case.

I'm still trying to solve this like this way...

Public Class MyUIForm
Inherits Form
Delegate Sub SetTextCallback([text] As String)

Private Sub DisplayText(ByVal [text] As String)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.

If Me.txtReaderData.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf DisplayText)
Me.Invoke(d, New Object() {[text]})
Else
Me.txtReaderData.Text += [text]
End If
End Sub

....but I don't know how to do binding with txtReaderData-TextBox of the
SerialPort reading-classes "ReaderData"-public property. Propably I
should not use txtReaderData.DataBindings.Add("Text", MyClass,
"ReaderData") any more, but how txtReaderData can notice when
"ReaderData"-property value changes?

That how to sample code
(ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclictl/html/138f38b6-1099-4fd5-910c-390b41cbad35.htm)
is simply too complex to figure out for someone - read: for me :) - who
is not yet familiar with threads handling in my mind.
 

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