Hi,

From MSDN documentation, it implies that any cross ui thread calls will
result in InvalidOperationException when one tries to access a UI object from
a non-creation thread.

I have a situation where someone is executing some code in a thread pool
(Threading.IsThreadPoolThread is true and so if Form.InvokeRequired is true)
and yet when it calls

// this.mainTabControl.InvokeRequired is true
TabControl tc = this.mainTabControl.TabPages[i];

it does not throw the InvalidOperationException, why? Is this because it is
not actually accessing the UI part of the control? If is like going through
its internal list?

Thanks.

Leon

Re: Question on Control.InvokeRequired and Cross UI Thread call? by marc

marc
Mon Oct 23 00:59:12 CDT 2006

Which framework? 2.0 is better at spotting this than 1.1.

In either event, this exception should really be seen as a warning shot
to the developer; I wouldn't assume that every Control implementation
is going to spot every cross-thread call, but hopefully it spots enough
of the common ones to make the developer realise they've forgotten
something...

Because after all, the UI thread could be adding pages at this point
;-p

Marc


Re: Question on Control.InvokeRequired and Cross UI Thread call? by newsgroup01

newsgroup01
Mon Oct 23 02:11:01 CDT 2006

> Which framework? 2.0 is better at spotting this than 1.1.
I can't recall there is any support to catch this kind of violation in .Net
1.1 but I know Control.InvokeRequired is in .Net 1.1. I am referring to .Net
2.

"marc.gravell@gmail.com" wrote:

> Which framework? 2.0 is better at spotting this than 1.1.
>
> In either event, this exception should really be seen as a warning shot
> to the developer; I wouldn't assume that every Control implementation
> is going to spot every cross-thread call, but hopefully it spots enough
> of the common ones to make the developer realise they've forgotten
> something...
>
> Because after all, the UI thread could be adding pages at this point
> ;-p
>
> Marc
>
>

RE: Question on Control.InvokeRequired and Cross UI Thread call? by GunnarValur

GunnarValur
Wed Oct 25 12:00:01 CDT 2006

From my understanding you only get this exception if you try to get the
controls handle and in your case it appears your are not trying to do that.

Property Control.Handle

There is also a property on controls called CheckForIllegalCrossThreadCalls.
If you set this property to false you will not get this exception even if
you access the Handle property

"Leon" wrote:

> Hi,
>
> From MSDN documentation, it implies that any cross ui thread calls will
> result in InvalidOperationException when one tries to access a UI object from
> a non-creation thread.
>
> I have a situation where someone is executing some code in a thread pool
> (Threading.IsThreadPoolThread is true and so if Form.InvokeRequired is true)
> and yet when it calls
>
> // this.mainTabControl.InvokeRequired is true
> TabControl tc = this.mainTabControl.TabPages[i];
>
> it does not throw the InvalidOperationException, why? Is this because it is
> not actually accessing the UI part of the control? If is like going through
> its internal list?
>
> Thanks.
>
> Leon
>

RE: Question on Control.InvokeRequired and Cross UI Thread call? by newsgroup01

newsgroup01
Thu Oct 26 09:31:03 CDT 2006

> From my understanding you only get this exception if you try to get the
> controls handle and in your case it appears your are not trying to do that.
Sorry not true. If you try to do this.textBox1.Text = "abce";

It will generate the exception.

> There is also a property on controls called CheckForIllegalCrossThreadCalls.
Yes, this is on when running in debugger. It is false if running outside.

I set it to true prior the Application.Run() to ensure I get this exception
always. Good for beta and testing.

> If you set this property to false you will not get this exception even if
> you access the Handle property
>
> "Leon" wrote:
>
> > Hi,
> >
> > From MSDN documentation, it implies that any cross ui thread calls will
> > result in InvalidOperationException when one tries to access a UI object from
> > a non-creation thread.
> >
> > I have a situation where someone is executing some code in a thread pool
> > (Threading.IsThreadPoolThread is true and so if Form.InvokeRequired is true)
> > and yet when it calls
> >
> > // this.mainTabControl.InvokeRequired is true
> > TabControl tc = this.mainTabControl.TabPages[i];
> >
> > it does not throw the InvalidOperationException, why? Is this because it is
> > not actually accessing the UI part of the control? If is like going through
> > its internal list?
> >
> > Thanks.
> >
> > Leon
> >

Re: Question on Control.InvokeRequired and Cross UI Thread call? by Roman

Roman
Thu Oct 26 10:55:55 CDT 2006

> > There is also a property on controls called CheckForIllegalCrossThreadCalls.
> Yes, this is on when running in debugger. It is false if running outside.

But be careful. The doc's say that there is no guaranty that this
exception will not be thrown
in non debugging mode.