473,763 Members | 3,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4704
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 SafeUpdateLabel Delegate ( string newText );
private void SafeUpdateLabel ( string newText )
{
myLabel.Text = newText;
}

private void UpdateLabel ( string newText )
{
if ( this.InvokeRequ ired )
this.Invoke( new SafeUpdateLabel Delegate( 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.comwro te:
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.co m>
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
4666
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 RichEditBox. To give you some idea of the approach I took... private static RichTextBox rtb; private delegate void RtbAppendTextHandler(string output); private static RtbAppendTextHandler RtbAppendText; Log.Init(RichTextBox richTextBox,...
4
3584
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 quite tiresome to have to write code to call MyControl.Invoke for each control on the form, along with the delegates that are required for each. Is there a better way to do this? What I mean is, if I could marshal the
2
1962
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 regularly to inform the main thread of the progress (which is then displayed to the user). Since the event is raised from another thread, i've used me.Invoke() to update the ui properly. However, my problem is in cancelling the process. I need to...
0
11702
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 java.sql.SQLException: Deadlock found when trying to get lock; Try restarting transaction message from server: "Lock wait timeout exceeded; try restarting transaction"; We get such errors generally on inserts or updates while applying a
94
30348
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 Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
1
8825
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 Exception:Deadlock found when trying to get lock; Try restarting transaction, message from server: "Lock wait timeout exceeded; Try restarting transaction" at com.xyz.retrieveDetails(MySessionBean.java:679) at...
2
4424
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 or simple). I am hoping the description will generate some ideas I can check out. PROBLEM: ----------------- Switching to UI thread using Invoke(), everything seems good.
7
2068
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 I modified my code to find out who locks the app. Therfore I added a string member "lockOwner" to the class in question and after each "lock(this)" I set the members value to the method name like: lock(this)
12
4498
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 within calls a Visual Basic input message box, which is synchronous, it waits until you enter some input, and press enter. But, this message box pops up every second regardless if the old ones have finished. The timer's Tick function is called over...
0
10140
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9992
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9935
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.