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

C#

 
QuestionMessage Removed Pin
17-May-10 10:02
professionalN_tro_P17-May-10 10:02 
AnswerRe: Nullable Type or On the fly calculations Pin
Dr.Walt Fair, PE17-May-10 10:34
professionalDr.Walt Fair, PE17-May-10 10:34 
AnswerRe: Nullable Type or On the fly calculations Pin
Richard MacCutchan17-May-10 10:35
mveRichard MacCutchan17-May-10 10:35 
AnswerRe: Nullable Type or On the fly calculations Pin
Luc Pattyn17-May-10 11:18
sitebuilderLuc Pattyn17-May-10 11:18 
QuestionThreads and TextBox Pin
CaesarCZ17-May-10 9:53
CaesarCZ17-May-10 9:53 
AnswerRe: Threads and TextBox Pin
Not Active17-May-10 9:59
mentorNot Active17-May-10 9:59 
GeneralRe: Threads and TextBox Pin
CaesarCZ17-May-10 10:25
CaesarCZ17-May-10 10:25 
AnswerRe: Threads and TextBox Pin
Luc Pattyn17-May-10 10:03
sitebuilderLuc Pattyn17-May-10 10:03 
Yes, the original code is thread-safe. This is how it works:
- thread 1 calls the method containing your snippet, I'll call it SetText();
- it detects invoke is required, launches a windows message to the GUI thread, and waits for it to return;
- the GUI thread now executes the same method SetText(), sees no invoke is required, sets the TextBox property, and returns;
- this causes thread 1, still waiting inside its SetText(), to continue.

Now everything the GUI thread does gets sequenced (well, not absolutely aacurate, but good enough here) through its event queue, so even if another thread (thread 2) were to call SetText, it would basically execute the same steps as listed above, and the common part, executed on the GUI thread, would be sequenced by the event queue.

So any number of SetText() calls will execute in the order they get called, without them interfering amongst themselves.

Adding a lock does not make it any safer; and as you discovered, if it covers the entire content of SetText(), it brings everything to a halt, as the basic concept of the construct is that SetText, when called on a thread other than the GUI thread, causes itself to be executed once more, on the GUI thread, which would be prevented by the very lock you added.


Smile | :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]

I only read formatted code with indentation, so please use PRE tags for code snippets.

I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).

GeneralRe: Threads and TextBox Pin
CaesarCZ17-May-10 10:21
CaesarCZ17-May-10 10:21 
GeneralRe: Threads and TextBox Pin
Luc Pattyn17-May-10 11:08
sitebuilderLuc Pattyn17-May-10 11:08 
GeneralRe: Threads and TextBox Pin
CaesarCZ18-May-10 2:28
CaesarCZ18-May-10 2:28 
Questionimage access using dll Pin
AnirbanChak 17-May-10 9:52
AnirbanChak 17-May-10 9:52 
AnswerRe: image access using dll Pin
Dr.Walt Fair, PE17-May-10 10:20
professionalDr.Walt Fair, PE17-May-10 10:20 
AnswerRe: image access using dll Pin
Alan Balkany18-May-10 4:18
Alan Balkany18-May-10 4:18 
QuestionJava to C# Pin
Zoomlion17-May-10 6:46
Zoomlion17-May-10 6:46 
GeneralRe: Java to C# Pin
harold aptroot17-May-10 7:04
harold aptroot17-May-10 7:04 
AnswerRe: Java to C# Pin
The Man from U.N.C.L.E.17-May-10 7:28
The Man from U.N.C.L.E.17-May-10 7:28 
Questionequivalent Pin
genieabdo17-May-10 4:52
genieabdo17-May-10 4:52 
AnswerRe: equivalent Pin
Abhinav S17-May-10 5:01
Abhinav S17-May-10 5:01 
AnswerRe: equivalent Pin
Luc Pattyn17-May-10 5:37
sitebuilderLuc Pattyn17-May-10 5:37 
GeneralRe: equivalent Pin
genieabdo17-May-10 5:57
genieabdo17-May-10 5:57 
GeneralRe: equivalent Pin
Luc Pattyn17-May-10 6:05
sitebuilderLuc Pattyn17-May-10 6:05 
AnswerRe: equivalent Pin
PIEBALDconsult17-May-10 6:05
mvePIEBALDconsult17-May-10 6:05 
GeneralRe: equivalent Pin
genieabdo17-May-10 6:11
genieabdo17-May-10 6:11 
GeneralRe: equivalent Pin
Luc Pattyn17-May-10 6:12
sitebuilderLuc Pattyn17-May-10 6:12 

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.