Question on Control.InvokeRequired and Cross UI Thread call?

G

Guest

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;

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
 
M

marc.gravell

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
 
G

Guest

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.
 
G

Guest

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
 
G

Guest

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 said:
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;

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
 
R

Roman Wagner

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.
 

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

Similar Threads


Top