-
January 1st, 2009, 08:02 PM
#1
[RESOLVED] Changing GUI from another thread
I have been working with C# for about 6 months now, and started working with threads last week. I created an application that has a couple of threads and one of these needs to change the gui. I can't seem to able to do this, so any help on the matter is greatly appreciated.
-
January 1st, 2009, 08:06 PM
#2
Re: Changing GUI from another thread
How many times does this have to be re-answered....
You can ONLY reference controls on the thread that created them. To perform a cross-thread GUI update, you must pass the information back to the UI thread, and let that thread do the work.
The simplest way is with the Invoke method that is supported by every class derived from Control.
See the threads here (A simple search on CG....)
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 1st, 2009, 08:52 PM
#3
Re: Changing GUI from another thread
I have tried that but it still doesn't work.
-
January 1st, 2009, 08:56 PM
#4
Re: Changing GUI from another thread
Post a small sample and specify the issues you are having.
-
January 1st, 2009, 09:16 PM
#5
Re: Changing GUI from another thread
Code:
BackgroundClass bgC = new BackgroundClass(this.Level);
new Thread(new ThreadStart(bgC.setLevel)).Start();
Thread - Started by above:
Code:
namespace ThreadedApp
{
class BackgroundClass
{
MainForm mf = new MainForm();
float battLevel;
public BackgroundClass(float Level)
{
this.battLevel = Level;
}
public void setLevel()
{
mf.setBatteyLvl(this.battLevel);
}
}
}
GUI:
Code:
namespace ThreadedApp
{
class MainForm
{
delegate void setBatteryLvlDelegate(float batteryLevel);
public MainForm()
{
InitializeComponent();
}
public void setBatteryLvl(float batteryLevel)
{
if (InvokeRequired)
{
this.Invoke(new setBatteryLvlDelegate(setBatteryLvl), new object[] { batteryLevel });
}
this.batteryLvl.Text = batteryLevel.ToString();
}
}
}
-
January 1st, 2009, 10:01 PM
#6
Re: Changing GUI from another thread
If Invoke is required, then you properly fire off the cross thread invoke, then fall immediately into the direct access.
You need to fix this or ELSE
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 2nd, 2009, 04:16 AM
#7
Re: Changing GUI from another thread
here is a simple example that may help you understand the nature of the problem...
It's not a bug, it's a feature!
-
January 2nd, 2009, 06:15 AM
#8
Re: Changing GUI from another thread
Originally Posted by foamy
here is a simple example that may help you understand the nature of the problem...
that helped, but what about if the method is in another class?
-
January 2nd, 2009, 06:21 AM
#9
Re: Changing GUI from another thread
Just make sure your UI class listens for the events that your delegate(s) fire, and react upon them. If you'd like, I might be able to dig up an example a little later on.
It's not a bug, it's a feature!
-
January 2nd, 2009, 06:35 AM
#10
Re: Changing GUI from another thread
don't worry about it, i just passed the instance of the gui through all of the threads to end up at the last one
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|