Thread questions

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 :(
 
M

mia lanui

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top