473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Updating GUI controls from threads

Hi,

I have a thread running, and at a point in this thread I am calling a
method to update GUI controls. From the net I have read that updating
GUI controls from the worker thread is bad practice.

Thread myThread = new Thread(new ThreadStart(Wor ker));...
myThread.Start( );
public void Worker()
{
....
updateGUI();
....
}
Can someone help me out.
Thanks in Advance

Nov 17 '05 #1
10 8444
Curious wrote:
I have a thread running, and at a point in this thread I am calling a
method to update GUI controls. From the net I have read that updating
GUI controls from the worker thread is bad practice.


It's not only bad practice, it's not even allowed in .NET 2. See Jon's
page for all the information you need about this:

http://www.yoda.arachsys.com/csharp/...winforms.shtml

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #2
Here are a couple more good articles on the topic:

http://msdn.microsoft.com/library/de...ms06112002.asp
http://msdn.microsoft.com/library/de...ms08162002.asp
http://msdn.microsoft.com/library/de...ms08162002.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Oliver Sturm" <ol****@sturmne t.org> wrote in message
news:xn******** ********@msnews .microsoft.com. ..
Curious wrote:
I have a thread running, and at a point in this thread I am calling a
method to update GUI controls. From the net I have read that updating
GUI controls from the worker thread is bad practice.


It's not only bad practice, it's not even allowed in .NET 2. See Jon's
page for all the information you need about this:

http://www.yoda.arachsys.com/csharp/...winforms.shtml

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #3
Thanks, I've made this mistake myelf, and wasn't even aware that it was a
mistake until I saw this post.

I've had a look at Jon's article but I still have one question...

The article begins with "Never invoke any method or property on a control
created on another thread". I can see why we shouldn't attempt to change the
control, but are we able to read from it? Can we invoke a "get" property? Can
we invoke a method which doesn't change the control?

Javaman

"Oliver Sturm" wrote:
Curious wrote:
I have a thread running, and at a point in this thread I am calling a
method to update GUI controls. From the net I have read that updating
GUI controls from the worker thread is bad practice.


It's not only bad practice, it's not even allowed in .NET 2. See Jon's
page for all the information you need about this:

http://www.yoda.arachsys.com/csharp/...winforms.shtml

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #4
Javaman59 <Ja*******@disc ussions.microso ft.com> wrote:
Thanks, I've made this mistake myelf, and wasn't even aware that it was a
mistake until I saw this post.

I've had a look at Jon's article but I still have one question...

The article begins with "Never invoke any method or property on a control
created on another thread". I can see why we shouldn't attempt to change the
control, but are we able to read from it? Can we invoke a "get" property? Can
we invoke a method which doesn't change the control?


No, you shouldn't. There's no guarantee it would be in a consistent
state; the changes may not be made atomically with respect to other
threads.

--
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 17 '05 #5
I was reading and trying the code in the link provided.

My problem is that I can't find the Invoke() method. I tried
System.Windows. Forms.Control but is not visible.

Where should this method exist?

Nov 17 '05 #6
> I was reading and trying the code in the link provided.

I copied it into notepad, and compiled it with the command line compiler
(csc), and it worked first time for me.
My problem is that I can't find the Invoke() method. I tried
System.Windows. Forms.Control but is not visible.


Invoke is an overloaded method in the System.Windows. Forms.Control class.
The Form class in the example inherits it.

It appears to me that your usage of qualified names
("System.Window s.Forms.Control ") is incorrect. AFAIK, you use qualified names
to designate a namespace (in a using clause), a class (in a declaration), or
to call a static class method. So "System.Windows .Forms.Form.Inv oke" will not
compile, even though Invoke is in the Form class, because Invoke is a
non-static method.

Regards,

Javaman.
Nov 17 '05 #7
> I was reading and trying the code in the link provided.

I pasted it into notepad, and compiled it from the command line with csc,
and it worked first time.
My problem is that I can't find the Invoke() method. I tried
System.Windows. Forms.Control but is not visible.


It is a non-static, overloaded method of the System.Windows. Forms.Control
class, so is inherited by the Form class in the example.

It looks to me as if you might be making a mistake with using qualified
names. You can only apply a qualified name (eg.
System.Windows. Forms.Control.I nvoke) to a static class method, so
System.Windows. Forms.Form.Invo ke will not compile, even though Invoke is a
method of the the Form class.

Regards,

Javaman
Nov 17 '05 #8
"Curious" <th****@mail.gl obal.net.mt> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
I was reading and trying the code in the link provided.

My problem is that I can't find the Invoke() method. I tried
System.Windows. Forms.Control but is not visible.

Where should this method exist?


It should definately be there!

http://msdn.microsoft.com/library/de...nvokeTopic.asp

You're trying it on an instance, and not as a static method, right?
Nov 17 '05 #9
I have a library, in this library I have the scenario given below. The
StartExecution( ) method is started in a new thread from the GUI
application. The method that is being called inside this
thread(worker) is to update GUI. Since its on a worker thread, its bad
practice(though it works). Now as read from the links given I tried to
use the Invoke() method, but I cannot find it.

namespace myLibrary
{
public delegate void UpdateListView( ListViewItem lvItem);

public class myClass
{
private UpdateListView updateEventQueu eListView;

public myClass(UpdateL istView ptr)
{
updateEventQueu eListview = ptr;
}

public void StartExecution( )
{
....
// Invoke is not being visible, accessed anywhere in this class
updateEventQueu eListView(myLis tViewItem);
....
}

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5769
by: Hennie de Nooijer | last post by:
Hi, Currently we're a building a metadatadriven datawarehouse in SQL Server 2000. We're investigating the possibility of the updating tables with enormeous number of updates and insert and the use of checkpoints (for simple recovery and Backup Log for full recovery). On several website people speak about full transaction log and the pace of growing can't keep up with the update. Therefore we want to create a script which flushes the...
3
5008
by: MLH | last post by:
I have a form, bound to a query. Its RecordSource property is a query named frmEnterLienAmounts. The form has a few bound controls and some unbound controls. The unbound controls are calculated fields whose DefaultValue property is set to something like DLookUp("","tblVehicleJobs","=Forms!frmEnterLienAmounts!VehicleJobID") The form (frmEnterLienAmounts) has, as one of its bound fields . As I scroll through the records, one-
11
2235
by: DW | last post by:
I've gotten this question a couple of times in interviews and I don't know what they are looking for: How do you update a control's property, such as a textbox.text property, from a thread, in .NET? It seems to me you just update it normally. What are the interviewers looking for here? Thanks. - W
0
1639
by: cwbp17 | last post by:
I'm having trouble updating individual datagrid cells. Have two tables car_master (columns include Car_ID, YEAR,VEHICLE) and car_detail (columns include Car_ID,PRICE,MILEAGE,and BODY);both tables have a FK relationship on CAR_ID so the oracledataadapter1 select statement(CommandText) is: select car_master.car_id, car_master.year,car_master.vehicle,car_detail.car_id AS EXPR1,
2
1266
by: Kejpa | last post by:
Hi, I've got a number of objects that each have it's own thread. When the value changes I raise an event. Now, I want to handle the event in a form and show the value in a listview. With my first approach I had to have a synclock on the listview the whole event handler, after some reading I've understood that updating a control from different threads should use the controls Invoke statement. Should I use it as well? It's not the...
0
993
by: Beorne | last post by:
Hello, I'm a Java and I'm new to C# (I've read something but I've no real programming experience). I have to realize in C# a form that displays some data that I read from ane external source (i.e. a Com port) ad displays it. The updating cycle is about every second. The Form has some controls that can be used. In Java there is the swing Timer class that implements a worker thread activated every n millisec and is synchronized with the...
5
3156
by: Beorne | last post by:
Hello, I'm a Java programmer and I'm new to C# (I've read something but I've no real programming experience). I'm programming a form that displays some data from an external source (i.e. a com port) ad displays it. The updating cycle is about every second. The windows form has a few controls. In Java there is the swing Timer class that implements a worker thread that is activated every n millisec and is synchronized with the interface...
0
1124
by: =?Utf-8?B?YmFrZXJzaGFjaw==?= | last post by:
Unless my app is EXTREMELY simple, I get the cross-threading error message regularly when updating controls on a Windows form. My latest example involves a dll that runs a System.Threading.Timer, updating private shared variables. When it finishes updating, it raises a NewData event. My app responds to that event by updating display controls (labels in this case). I have to use a delegate and update them like this: Private Sub...
2
5717
by: =?Utf-8?B?TUNN?= | last post by:
I have an asp.net page that contains an update panel. Within the update panel, controls get added dynamically. During partial page post backs the controls within the panel will change. I have a javascript function that uses the ClientID of the dynamic controls to perform certain operations on the client-side. With each partial page post back, I dynamically recreate the javascript function using the ClientIDs of the newly added controls....
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10329
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
10152
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
10092
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
8974
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
6740
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
5381
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2880
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.