Click here to Skip to main content
15,897,718 members
Home / Discussions / C#
   

C#

 
GeneralRe: cross thread operation not valid, when closing a windows form Pin
Henry Minute17-Apr-09 2:59
Henry Minute17-Apr-09 2:59 
GeneralRe: cross thread operation not valid, when closing a windows form Pin
But_Im_a_Lady20-Apr-09 1:16
But_Im_a_Lady20-Apr-09 1:16 
AnswerRe: cross thread operation not valid, when closing a windows form Pin
12Code17-Apr-09 2:06
12Code17-Apr-09 2:06 
GeneralRe: cross thread operation not valid, when closing a windows form Pin
But_Im_a_Lady17-Apr-09 2:15
But_Im_a_Lady17-Apr-09 2:15 
GeneralRe: cross thread operation not valid, when closing a windows form Pin
But_Im_a_Lady17-Apr-09 2:22
But_Im_a_Lady17-Apr-09 2:22 
AnswerRe: cross thread operation not valid, when closing a windows form Pin
0x3c017-Apr-09 2:46
0x3c017-Apr-09 2:46 
GeneralRe: cross thread operation not valid, when closing a windows form Pin
But_Im_a_Lady17-Apr-09 2:54
But_Im_a_Lady17-Apr-09 2:54 
AnswerRe: cross thread operation not valid, when closing a windows form Pin
Luc Pattyn17-Apr-09 4:01
sitebuilderLuc Pattyn17-Apr-09 4:01 
Hi,

here is my standard reply on cross-thread problems, I hope it helps:

Controls are not thread-safe, hence they should be touched (that is: their methods or properties called) only by the thread that created them, which normally is the main thread (aka GUI thread). Creating some controls on a different thread is unlikely to be successful, since all Controls get linked somehow: they reside on Forms, Forms are related to each other (by Parent, by Z-Order, etc), so normally all are created on a single thread.

If you violate the “don’t touch Controls from another thread” rule and are running .NET version 2.0 or above you will get an InvalidOperationException (“Cross-thread operation not valid”), which should be remedied by changing the code.

Do not set Control.CheckForIllegalCrossThreadCalls false, since that does hide the exception but does not cure the fundamental flaw in your code, so it just postpones the moment of failure, which typically will show as a non-responsive and possibly badly painted GUI.

Here are some ways to get another thread:
- explicitly launching a Thread instance
- exclicitly delegating some work to a ThreadPool thread
- using a BackgroundWorker; a BGW is a separate thread with the advantage that two of its events (ProgressChanged and RunWorkerCompleted) execute on the GUI thread; however the bulk of the work normally is handled in the DoWork handler which runs on a distinct thread.
- using timers other than System.Windows.Forms.Timer; the Forms timer ticks on the GUI thread, all other use different threads to handle the periodic event;
- using asynchronous input/output, such as the DataReceived event of the SerialPort class

Any of these touching a single method or property of a Control is sufficient to create havoc; there are 5 exceptions:
- the InvokeRequired property
- the Invoke, BeginInvoke, EndInvoke and CreateGraphics methods (the latter only if the handle for the control has already been created).

If there is a need to touch the Control from another thread, one must use an Invoke pattern, which basically looks like this:

public void SetText(string text) {
    if (myControl.InvokeRequired) {
        // this runs on the foreign thread and causes the
        // invocation of this same method on the GUI thread
        myControl.Invoke(new Action< string >(SetText),
            new object[] {text});
    } else {
        // this runs on the GUI thread only
        myControl.Text=text;
    }
}


Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


QuestionNeed Help For Reading and Sending SMS Through SonyEricsson Mobile Using C# Language Pin
A.Q.Ghouri17-Apr-09 1:15
A.Q.Ghouri17-Apr-09 1:15 
AnswerRe: Need Help For Reading and Sending SMS Through SonyEricsson Mobile Using C# Language Pin
Rajesh Anuhya17-Apr-09 1:22
professionalRajesh Anuhya17-Apr-09 1:22 
GeneralRe: Need Help For Reading and Sending SMS Through SonyEricsson Mobile Using C# Language Pin
A.Q.Ghouri20-Apr-09 18:06
A.Q.Ghouri20-Apr-09 18:06 
AnswerRe: Need Help For Reading and Sending SMS Through SonyEricsson Mobile Using C# Language Pin
Giorgi Dalakishvili17-Apr-09 2:07
mentorGiorgi Dalakishvili17-Apr-09 2:07 
GeneralRe: Need Help For Reading and Sending SMS Through SonyEricsson Mobile Using C# Language Pin
A.Q.Ghouri20-Apr-09 18:29
A.Q.Ghouri20-Apr-09 18:29 
QuestionWhy the line disappear? Pin
lune1217-Apr-09 1:15
lune1217-Apr-09 1:15 
AnswerRe: Why the line disappear? Pin
Luc Pattyn17-Apr-09 1:18
sitebuilderLuc Pattyn17-Apr-09 1:18 
GeneralRe: Why the line disappear? Pin
Henry Minute17-Apr-09 1:24
Henry Minute17-Apr-09 1:24 
GeneralU R D) Pin
Luc Pattyn17-Apr-09 1:38
sitebuilderLuc Pattyn17-Apr-09 1:38 
QuestionRe: Why the line disappear? Pin
lune1217-Apr-09 1:36
lune1217-Apr-09 1:36 
AnswerRe: Why the line disappear? Pin
Luc Pattyn17-Apr-09 1:40
sitebuilderLuc Pattyn17-Apr-09 1:40 
QuestionRe: Why the line disappear? Pin
lune1217-Apr-09 1:56
lune1217-Apr-09 1:56 
AnswerRe: Why the line disappear? Pin
Luc Pattyn17-Apr-09 2:02
sitebuilderLuc Pattyn17-Apr-09 2:02 
AnswerRe: Why the line disappear? Pin
0x3c017-Apr-09 1:35
0x3c017-Apr-09 1:35 
GeneralRe: Why the line disappear? Pin
Letsan17-Apr-09 4:32
Letsan17-Apr-09 4:32 
Questionhow to compare textbox text and database table field Pin
demetter17-Apr-09 0:43
demetter17-Apr-09 0:43 
AnswerRe: how to compare textbox text and database table field Pin
J a a n s17-Apr-09 1:08
professionalJ a a n s17-Apr-09 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.