471,594 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

lock or invoke is better for updating control.

Hi all,

I am having application wherein i am using asynchronous programming and
using callback i am updating one label control.

My question is can i use lock statment to lock control. If yes which
one is better to prevent unpredictable update of label. Whether should
i use lock statement or control.invoke and give method name.

Please correct me if i am wrong.

thanks in advance.

Nov 11 '06 #1
2 4539
archana wrote:
Hi all,

I am having application wherein i am using asynchronous programming and
using callback i am updating one label control.

My question is can i use lock statment to lock control. If yes which
one is better to prevent unpredictable update of label. Whether should
i use lock statement or control.invoke and give method name.

Please correct me if i am wrong.

thanks in advance.
Hi,

You will have to use Control.Invoke. Cross-thread calls to update your UI
are not permitted.

Using Control.Invoke will marshal the call into the UI thread, and so,
updating your label will be safe, since your update method cannot run
side-by-side. Don't use the lock statement.

///
private delegate void SafeUpdateLabelDelegate ( string newText );
private void SafeUpdateLabel ( string newText )
{
myLabel.Text = newText;
}

private void UpdateLabel ( string newText )
{
if ( this.InvokeRequired )
this.Invoke( new SafeUpdateLabelDelegate( SafeUpdateLabel ), newText );
else
SafeUpdateLabel( newText );
}
///

In the above example, calling UpdateLabel will update the label safely, by
either marshalling the call to SafeUpdateLabel to the UI thread (via
this.Invoke) or calling the method directly, if an invoke is not required.

--
Hope this helps,
Tom Spink

Google first, ask later.
Nov 11 '06 #2
archana <tr**************@yahoo.comwrote:
I am having application wherein i am using asynchronous programming and
using callback i am updating one label control.

My question is can i use lock statment to lock control. If yes which
one is better to prevent unpredictable update of label. Whether should
i use lock statement or control.invoke and give method name.

Please correct me if i am wrong.
Locking would not be sufficient. You *must* update UI components on the
UI thread - there's no reliable alternative.

See http://www.pobox.com/~skeet/csharp/winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Don | last post: by
94 posts views Thread by Samuel R. Neff | last post: by
2 posts views Thread by =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post: by
7 posts views Thread by Rainer Queck | last post: by
12 posts views Thread by Zytan | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.