Error Help: Cross-thread operation not valid: Control ” accessed from a thread other than the thread it was created on

http://www.eggheadcafe.com/software/aspnet/29408264/crossthread-operation-not-valid-control–accessed-from-a-thread-other-than-the-thread-it-was-created-on.aspx

Summary
Joe inquired about Cross-thread operation not valid: Control ” accessed from a thread other than the thread it was created on. Joe, Amar, Jeta, and Willy Denoyette [MVP] believed EventHandler, ThreadPool, EventArgs, BeginInvoke, and Control may be crucial. In fact, Thread, Win32, and Stack may also be significant.
Detail
I’ve been getting this message at all different times since a few days ago.

I understand the message but not why I get it. I don’t have any other

threads.

This happens when closing dialog boxes, doing a drag and drop and a few

other things which I don’t remember right now.

It only happens while I’m running within the IDE and only seems to be on my

computer. We’ve tested the same thing on other machines running VS.2005 and

there is no problem.

Any ideas? I’m thinking I need to reinstall VS.

Thanks,

Joe

Solution:

Amar chimed in with hello joe.

Reinstalling VS wont solve the problem.

The problem you are facing is because microsoft finally decided to

enforce strict restriction on cross thread acces of control..(i.e you

cannot acces the control from other threads than on which it was

created on)

There are two solutions for this..

1> The lengthy and the better solution ..

Using the InvokeRequired property of the control.

Eg.

private void Form1_Load(object sender, EventArgs e)

{

if(InvokeRequired)

{

this.Invoke(new EventHandler(Form1_Load), new object[]

{sender, e});

return;

}

//Your code here…

}

2>

Just set the Control.CheckForIllegalCrossThreadCalls property to

false.

This willstop the exception being thrown but wont solve the problem.

I would recommend the first way.

jeta mentioned hi Joe ,
Amar has provided the detailed correct reply to you.
Note: Microsoft enforces this restriction to help us to detect the invalid
operation: accessing the UI control methods/properties from another thread.
This is the GUI threading rule, which is introduced by history reason(.Net
Winform controls encapsulates Win32 controls which are COM STA threading
model. This threading model requires the accessing to the controls can only
be allowed from the creating thread).
So the first solution is highly recommended and second way is turning off
the Microsoft's help.
Thanks.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s