Click here to Skip to main content
15,887,821 members
Home / Discussions / C#
   

C#

 
QuestionRe: Threading problem Pin
Vikram A Punathambekar20-May-08 18:35
Vikram A Punathambekar20-May-08 18:35 
AnswerRe: Threading problem Pin
MarkB77720-May-08 18:47
MarkB77720-May-08 18:47 
GeneralRe: Threading problem Pin
Vikram A Punathambekar20-May-08 18:57
Vikram A Punathambekar20-May-08 18:57 
GeneralRe: Threading problem Pin
MarkB77720-May-08 19:03
MarkB77720-May-08 19:03 
GeneralRe: Threading problem Pin
Laddie20-May-08 19:01
Laddie20-May-08 19:01 
GeneralRe: Threading problem Pin
MarkB77720-May-08 19:07
MarkB77720-May-08 19:07 
AnswerRe: Threading problem Pin
Laddie20-May-08 18:47
Laddie20-May-08 18:47 
AnswerRe: Threading problem Pin
Mike Dimmick21-May-08 6:55
Mike Dimmick21-May-08 6:55 
There really isn't a simple answer.

The problem is that the background thread may well be updating it, but that doesn't necessarily mean that the main thread is actually going to read it. You have to synchronize the execution of the two threads for the update to be seen by the other thread.

If the 'main' thread is running UI, as the others have said, one approach is to send a message to the main thread using Control.Invoke. It's often better to send the data that's changed in a parameter of the method that's invoked.

Otherwise the tidiest approach generally is to use locks, for example, the Monitor class, whenever the value is read from or written to (you must do both, and you must lock the same object in all situations). However, you now have to be aware of the possibility of deadlock - any time more than one lock is acquired at the same time, you can get into a situation where one thread owns lock A and needs lock B, while another owns lock B and needs lock A (bigger situations where multiple threads all own some locks and need others can also exist, this is just the smallest simplest example). Each thread ends up waiting for the other, neither can proceed, and generally the program becomes unresponsive. The only true way to avoid a deadlock is to ensure that locks are always acquired in the same order, where multiple locks are acquired by the same thread. This generally leads to fewer, coarse-grained locks. However, this can cause these locks to become 'hot-spots', where threads end up waiting for one another to release commonly-used locks. In the worst case you can get a 'lock convoy' where the threads never actually run in parallel, instead they run in turn as each acquires a lock and all the others wait on that lock.

Lock-free programming is even harder.

I would generally be happy not to write multithreaded programs or if I do, to rely solely on messaging. The more highly concurrent you get, the more complicated the system becomes and the higher chance of a race condition (as you've seen) or a deadlock.


DoEvents: Generating unexpected recursion since 1991

GeneralRe: Threading problem Pin
MarkB77721-May-08 13:50
MarkB77721-May-08 13:50 
QuestionHandling Click and DoubleClick events differently Pin
Dr.Walt Fair, PE20-May-08 16:32
professionalDr.Walt Fair, PE20-May-08 16:32 
AnswerRe: Handling Click and DoubleClick events differently Pin
Vikram A Punathambekar20-May-08 18:40
Vikram A Punathambekar20-May-08 18:40 
GeneralRe: Handling Click and DoubleClick events differently Pin
Dr.Walt Fair, PE21-May-08 1:57
professionalDr.Walt Fair, PE21-May-08 1:57 
AnswerRe: Handling Click and DoubleClick events differently Pin
Atif Ali Bhatti20-May-08 19:30
Atif Ali Bhatti20-May-08 19:30 
GeneralRe: Handling Click and DoubleClick events differently Pin
Vikram A Punathambekar20-May-08 19:39
Vikram A Punathambekar20-May-08 19:39 
GeneralRe: Handling Click and DoubleClick events differently Pin
Atif Ali Bhatti20-May-08 19:50
Atif Ali Bhatti20-May-08 19:50 
GeneralRe: Handling Click and DoubleClick events differently Pin
Dr.Walt Fair, PE21-May-08 2:02
professionalDr.Walt Fair, PE21-May-08 2:02 
GeneralRe: Handling Click and DoubleClick events differently Pin
Dr.Walt Fair, PE21-May-08 17:27
professionalDr.Walt Fair, PE21-May-08 17:27 
GeneralRe: Handling Click and DoubleClick events differently Pin
Atif Ali Bhatti21-May-08 18:29
Atif Ali Bhatti21-May-08 18:29 
GeneralRe: Handling Click and DoubleClick events differently Pin
Dr.Walt Fair, PE22-May-08 13:44
professionalDr.Walt Fair, PE22-May-08 13:44 
GeneralRe: Handling Click and DoubleClick events differently Pin
Dr.Walt Fair, PE21-May-08 1:47
professionalDr.Walt Fair, PE21-May-08 1:47 
QuestionSMTPServer and ProcessStartInfo Pin
versamps20-May-08 15:18
versamps20-May-08 15:18 
AnswerRe: SMTPServer and ProcessStartInfo Pin
Peter Josefsson Sweden21-May-08 13:56
Peter Josefsson Sweden21-May-08 13:56 
QuestionError message- Expected class delegate,enum,interface or struct Pin
Angelinna20-May-08 14:07
Angelinna20-May-08 14:07 
AnswerRe: Error message- Expected class delegate,enum,interface or struct Pin
carbon_golem20-May-08 14:19
carbon_golem20-May-08 14:19 
AnswerRe: Error message- Expected class delegate,enum,interface or struct Pin
Guffa20-May-08 14:20
Guffa20-May-08 14:20 

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.