Home All Groups Group Topic Archive Search About

CheckForIllegalCrossThreadCalls ?

Author
25 Jan 2006 1:39 PM
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?

Author
25 Jan 2006 1:57 PM
Armin Zingler
"Mika M" <mahmik_removet***@luukku.com> schrieb
> 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
Author
26 Jan 2006 9:43 AM
Mika M
Armin Zingler wrote:

Show quoteHide quote
>> 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.
>

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.

--
Mika
Author
26 Jan 2006 11:04 AM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Mika M" <mahmik_removet***@luukku.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
26 Jan 2006 12:01 PM
Armin Zingler
Show quote Hide quote
"Mika M" <mahmik_removet***@luukku.com> schrieb
> Armin Zingler wrote:
>
> > > 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.
> >
>
> 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
Author
27 Jan 2006 9:20 AM
Mika M
Mika M wrote:
Show quoteHide quote
> Armin Zingler wrote:
>
>>> 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.
>>
>
> 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.