473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 4678
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: User N | last post by:
I have a log class with static methods that grab and release a mutex as required. The log class is designed to be called from both GUI and thread pool threads, and dump output to a logfile and a...
4
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is...
2
by: Don | last post by:
How to stop a process which is running in a separate thread!!! I've got a class which performs some lengthy process in a background (separate) thread. And this lengthy process raises events...
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
1
by: Arif Mohammed | last post by:
Hi, iam using MySql 4.0.1 and jboss-4.0.4.GA Iam getting the following exception when there are more concurrent requests more than 50 in a second Caused by: org.xyz.MyClass: SQL...
2
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small...
7
by: Rainer Queck | last post by:
Hello NG, my application hangs onec a while. When I interrupt the execution (press the || button) the debugger shows me a green arrow on a "lock(this)" line. I first thought, well "deadlock" so...
12
by: Zytan | last post by:
I have a Timer class set to trigger every second. The Tick function that is called every second uses a lock to prevent multiple ticks from executing the same code at the same time. The code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.