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

Accessing GUI from threads

In my main dialog, which opens when my application starts
and never closes until the application completes, I have a
list box. My assembly also contains several threads. I
would like to write to the list box from my main
application as well as all the other threads. In my main
application I have a public method that adds a new item to
the listbox each time it's called. My constructor makes
the dialog object public. My intent is to call my
function from anywhere using the syntax:

MyClass.mainDialog.AddStringToList(someString);

It seems to work for the one thread I've tested it with
but I'm guessing on a lot of this. I've heard that
threads can't, or at least shouldn't access GUI, although
I'm not sure why. Does anyone forsee any problems or have
any references recommendations to explain why there are
problems with threads and GUI, and how to deal with them?
Thanks for your help!

Here is the stripped-down code:

public class MyClass : System.Windows.Forms.Form
{
public static MyClass mainDialog;

public MyClass()
{
mainDialog = this;
}

public void AddStringToList(string s)
{
this.myListBox.Items.Add(s);
}
}
Nov 15 '05 #1
2 1547
You need to use Control.Invoke to update UI objects from another thread.
They need to be updated on the thread that created the control. The Control
Class documentation states:

Thread Safety
Only the following members are safe for multithreaded operations:
BeginInvoke, EndInvoke, Invoke, InvokeRequired, and CreateGraphics.
"Ray Mitchell" <an*******@discussions.microsoft.com> wrote in message
news:07****************************@phx.gbl...
In my main dialog, which opens when my application starts
and never closes until the application completes, I have a
list box. My assembly also contains several threads. I
would like to write to the list box from my main
application as well as all the other threads. In my main
application I have a public method that adds a new item to
the listbox each time it's called. My constructor makes
the dialog object public. My intent is to call my
function from anywhere using the syntax:

MyClass.mainDialog.AddStringToList(someString);

It seems to work for the one thread I've tested it with
but I'm guessing on a lot of this. I've heard that
threads can't, or at least shouldn't access GUI, although
I'm not sure why. Does anyone forsee any problems or have
any references recommendations to explain why there are
problems with threads and GUI, and how to deal with them?
Thanks for your help!

Here is the stripped-down code:

public class MyClass : System.Windows.Forms.Form
{
public static MyClass mainDialog;

public MyClass()
{
mainDialog = this;
}

public void AddStringToList(string s)
{
this.myListBox.Items.Add(s);
}
}

Nov 15 '05 #2
Here's a "Multithreaded Windows Forms Control Sample" that may help:

http://msdn.microsoft.com/library/de...rmscontrol.asp
threads can't, or at least shouldn't access GUI, although
I think what you are referring to is something I've seen somewhere in the
Asynchronous documentation:

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

It referers to one way of making asynchronous calls: Begin Invoke and End
Invoke as not recommended for UI's because like a synchronous call it blocks
until the operation completes. It says Callbacks, polling
IAsyncResult.IsCompleted, or BeginInvoke WaitHandle EndInvoke are preferred.

--

"Ray Mitchell" <an*******@discussions.microsoft.com> wrote in message
news:07****************************@phx.gbl... In my main dialog, which opens when my application starts
and never closes until the application completes, I have a
list box. My assembly also contains several threads. I
would like to write to the list box from my main
application as well as all the other threads. In my main
application I have a public method that adds a new item to
the listbox each time it's called. My constructor makes
the dialog object public. My intent is to call my
function from anywhere using the syntax:

MyClass.mainDialog.AddStringToList(someString);

It seems to work for the one thread I've tested it with
but I'm guessing on a lot of this. I've heard that
threads can't, or at least shouldn't access GUI, although
I'm not sure why. Does anyone forsee any problems or have
any references recommendations to explain why there are
problems with threads and GUI, and how to deal with them?
Thanks for your help!

Here is the stripped-down code:

public class MyClass : System.Windows.Forms.Form
{
public static MyClass mainDialog;

public MyClass()
{
mainDialog = this;
}

public void AddStringToList(string s)
{
this.myListBox.Items.Add(s);
}
}

Nov 15 '05 #3

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

Similar topics

17
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
2
by: James Zhuo | last post by:
Accessing Items in a Collection works with the as suggested by Kevin and Chris (thx) But when i tried to use the same technic with databinding within the aspx file, it doesn't seem to work. This...
3
by: Alex | last post by:
I'm having a problem porting an ASP solution to ASPX. In the ASP solution I'm accessing a DCOM server, create sub DCOM objects and call functions from VB script on the ASP pages. The DCOM object...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
2
by: Alan Kemp | last post by:
Hi, I have a problem that is half python, half design. I have a multithreaded network server working, each client request spawns a new thread which deals with that client for as long as it is...
2
by: Jon C | last post by:
Hi, I've written a multi-threaded web spider which stores search results in a Shared ArrayList. Since each thread can launch further threads, the problem I have is pausing my main sub until all...
2
by: Joel Moore | last post by:
I'm looking at my debugger right now and can clearly see that two threads have their execution pointer residing within the same Synclock block. One thread is executing a Monitor.Wait(). I know...
5
by: Hyun-jik Bae | last post by:
I got a curiosity while using CLR. How can it be done that accessing object data by CLR application is safe even while garbage collector is working? Is the GC thread running in realtime priority...
1
by: Peter | last post by:
Hi, I have a Dictionary<key, valuewhich is accessed by three threads. One thread puts my value objects in the dictionary (occasionally), and also updates the contents of existing value objects -...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.