Click here to Skip to main content
15,890,512 members
Home / Discussions / C#
   

C#

 
QuestionTimestamp proble Pin
avi_dadi200222-Apr-09 10:25
avi_dadi200222-Apr-09 10:25 
AnswerRe: Timestamp proble Pin
Christian Graus22-Apr-09 10:54
protectorChristian Graus22-Apr-09 10:54 
AnswerRe: Timestamp proble Pin
Mycroft Holmes22-Apr-09 14:26
professionalMycroft Holmes22-Apr-09 14:26 
QuestionThreading problem Pin
Sajjad Izadi22-Apr-09 8:42
Sajjad Izadi22-Apr-09 8:42 
AnswerRe: Threading problem Pin
0x3c022-Apr-09 8:55
0x3c022-Apr-09 8:55 
GeneralRe: Threading problem Pin
Sajjad Izadi22-Apr-09 9:06
Sajjad Izadi22-Apr-09 9:06 
GeneralRe: Threading problem Pin
0x3c022-Apr-09 9:28
0x3c022-Apr-09 9:28 
AnswerRe: Threading problem Pin
Luc Pattyn22-Apr-09 8:56
sitebuilderLuc Pattyn22-Apr-09 8:56 
Hi,

you get one of my standard replies:

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 in C# basically looks like this (VB.NET would be very similar):

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


GeneralRe: Threading problem Pin
Fayu22-Apr-09 9:10
Fayu22-Apr-09 9:10 
GeneralRe: Threading problem Pin
0x3c022-Apr-09 9:29
0x3c022-Apr-09 9:29 
GeneralRe: Threading problem Pin
Luc Pattyn22-Apr-09 9:38
sitebuilderLuc Pattyn22-Apr-09 9:38 
GeneralRe: Threading problem Pin
Fayu22-Apr-09 10:04
Fayu22-Apr-09 10:04 
QuestionHelp with generics [modified] Pin
GuimaSun22-Apr-09 8:17
GuimaSun22-Apr-09 8:17 
AnswerRe: Help with generics Pin
0x3c022-Apr-09 8:35
0x3c022-Apr-09 8:35 
AnswerRe: Help with generics Pin
Thomas Weller22-Apr-09 9:07
Thomas Weller22-Apr-09 9:07 
GeneralRe: Help with generics Pin
GuimaSun22-Apr-09 9:30
GuimaSun22-Apr-09 9:30 
GeneralRe: Help with generics Pin
0x3c022-Apr-09 9:52
0x3c022-Apr-09 9:52 
GeneralRe: Help with generics Pin
GuimaSun22-Apr-09 9:56
GuimaSun22-Apr-09 9:56 
GeneralRe: Help with generics Pin
Thomas Weller22-Apr-09 20:22
Thomas Weller22-Apr-09 20:22 
GeneralRe: Help with generics Pin
0x3c022-Apr-09 9:30
0x3c022-Apr-09 9:30 
Questionpublically readonly but internally writable objects? Pin
student_rhr22-Apr-09 7:43
student_rhr22-Apr-09 7:43 
AnswerRe: publically readonly but internally writable objects? Pin
harold aptroot22-Apr-09 7:49
harold aptroot22-Apr-09 7:49 
AnswerRe: publically readonly but internally writable objects? Pin
Thomas Weller22-Apr-09 8:33
Thomas Weller22-Apr-09 8:33 
AnswerRe: publically readonly but internally writable objects? Pin
0x3c022-Apr-09 8:36
0x3c022-Apr-09 8:36 
QuestionSerialization and ViewState Pin
Fayu22-Apr-09 7:11
Fayu22-Apr-09 7:11 

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.