using lock

  • Thread starter Thread starter PiotrKolodziej
  • Start date Start date
P

PiotrKolodziej

Hi
I'am writing a multithread application. This application has let say label1.
This label of course has it's own thread.
Then the question is:
Can i read label1.Text without invoking it just by using such a syntax in
other threads?:
lock(label1)
{
.....
}

Does it prevent from cross-read/write operations?
And one more short question is if i have to use this.invoke when i want to
write to this label1 ( if in previous example it wasn't necessary )?

Thanks or any suggestions
PK
 
PK,

You must use Invoke or BeginInvoke to initiate any access on a Form or
Control from a thread other than the main UI thread. The only
exceptions to that rule are those members that are safe for
multithreaded operations and there aren't many. Not only is the Text
property unsafe for multithreaded operations, but it has
thread-affinity requirements as well which pretty much eliminates the
possibility of using any of the synchronization primitives to guard its
access.

Brian
 
You must use Invoke or BeginInvoke to initiate any access on a Form or
Control from a thread other than the main UI thread. The only
exceptions to that rule are those members that are safe for
multithreaded operations and there aren't many. Not only is the Text
property unsafe for multithreaded operations, but it has
thread-affinity requirements as well which pretty much eliminates the
possibility of using any of the synchronization primitives to guard its
access.

Brian

What if the other thread is the MainThread event? Do i really have to invoke
the reads on label1?
 
PiotrKolodziej said:
What if the other thread is the MainThread event? Do i really have to invoke
the reads on label1?

I'm not sure what you mean by the MainThread event. But, yes you have
to invoke reads when InvokeRequired is true. InvokeRequired will be
false when execute on the main UI thread. Otherwise it will be true.

Brian
 

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

Back
Top