473,386 Members | 1,712 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,386 software developers and data experts.

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(Worker));...
myThread.Start();
public void Worker()
{
....
updateGUI();
....
}
Can someone help me out.
Thanks in Advance

Nov 17 '05 #1
10 8408
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****@sturmnet.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*******@discussions.microsoft.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.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 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.Windows.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.Invoke" 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.Invoke) to a static class method, so
System.Windows.Forms.Form.Invoke will not compile, even though Invoke is a
method of the the Form class.

Regards,

Javaman
Nov 17 '05 #8
"Curious" <th****@mail.global.net.mt> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.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 updateEventQueueListView;

public myClass(UpdateListView ptr)
{
updateEventQueueListview = ptr;
}

public void StartExecution()
{
....
// Invoke is not being visible, accessed anywhere in this class
updateEventQueueListView(myListViewItem);
....
}

Nov 17 '05 #10

You need to run Invoke on the control that is being called to from the
thread. If this is your own control, you can simply do it from the method
itself:

public class MyControl: Control {
...
delegate void MyMethodDelegate(string param);

public void MyMethod(string param) {
// This method supposedly access properties in the control,
// so calls should be marshalled onto the GUI thread.
if (InvokeRequired) {
Invoke(new MyMethodDelegate(MyMethod), param);
return;
}

// Now do whatever the method is really supposed to do
...
}
...
}

If it's not your control, it's also possible to call Invoke from the
outside. Both the InvokeRequired property and the Invoke method are
public, so you can call them from the outside. In your class, if I
understand your sample code correctly (which seems to be nonsense because
you have the field named "updateEventQueueListView", but then you suddenly
call a method called the same):

delegate void UpdateListViewDelegate(ListViewItem item);

if (updateEventQueueListView.InvokeRequired) {
updateEventQueueListView.Invoke(new UpdateListViewDelegate(
updateEventQueueListView.UpdateListView), item);
}

.... or something like that - I'm guessing at a lot of your types and
methods here.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #11

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

Similar topics

6
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...
3
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...
11
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...
0
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...
2
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...
0
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....
5
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...
0
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,...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.