Thread questions

  • Thread starter Thread starter chris
  • Start date Start date
C

chris

Hi, I have two question about threads:
First,
I "share" one instance of the class between main application thread (UI)and
two other worker threads, so there is a possibilty that they can access my
object at the same time. I would like to prevent this situation using "lock"
statments, is it ok? Do I have to "lock" all code where worker thread access
object or only this statments where thread change (modify) shared object?

Second,
I've created some events in thread class, event handlers are placed in main
thread where they update UI controls based on information in
DataReceiveEventArgs.
if (OnDataReceive != null)
{
DataReceiveEventArgs e = new DataReceiveEventArgs("message from thread");
OnDataReceive(this, e);
//invoke?
}
should I call invoke method?

Thanks for any help!
Sorry for my bad English :(
 
Hi, I have two question about threads:
First,
I "share" one instance of the class between main application thread (UI)and
two other worker threads, so there is a possibilty that they can access my
object at the same time. I would like to prevent this situation using "lock"
statments, is it ok? Do I have to "lock" all code where worker thread access
object or only this statments where thread change (modify) shared object?

lock() them all, unless you are ok with dirty reads.

You might also want to investigate the ReaderWriterLock in MSDN
 
Back
Top