CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2008
    Location
    .NET2.0 / .NET3.5 > VS2008
    Posts
    18

    [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.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  3. #3
    Join Date
    May 2008
    Location
    .NET2.0 / .NET3.5 > VS2008
    Posts
    18

    Exclamation Re: Changing GUI from another thread

    I have tried that but it still doesn't work.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Changing GUI from another thread

    Post a small sample and specify the issues you are having.

  5. #5
    Join Date
    May 2008
    Location
    .NET2.0 / .NET3.5 > VS2008
    Posts
    18

    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();
    		}
    	}
    }

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  7. #7
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    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!

  8. #8
    Join Date
    May 2008
    Location
    .NET2.0 / .NET3.5 > VS2008
    Posts
    18

    Re: Changing GUI from another thread

    Quote Originally Posted by foamy View Post
    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?

  9. #9
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    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!

  10. #10
    Join Date
    May 2008
    Location
    .NET2.0 / .NET3.5 > VS2008
    Posts
    18

    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
  •  





Click Here to Expand Forum to Full Width

Featured