Update label text from another thread

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2007
Posts: 23
Reputation: Wiizl is an unknown quantity at this point 
Solved Threads: 0
Wiizl Wiizl is offline Offline
Newbie Poster

Update label text from another thread

 
0
  #1
Mar 21st, 2009
I'm imitating progressbar value change like:
  1. private void playAnimation()
  2. {
  3. for(int i=1;i<=100;i++)
  4. {
  5. this.progressBar1.Value = i;
  6. System.Threading.Thread.Sleep(20);
  7. percent = i;
  8. }
  9. }
At the same time I try to run another thread, to update label text like "...% completed". Of course I couldn't change text of label that was created in main thread ( I know it's possible using delegates or something like that..), so I created label in the new thread.
  1. private static void percentage()
  2. {
  3. Label threadLabel = new Label();
  4. Point p = new Point(418,290);
  5. threadLabel.Location = p;
  6. threadLabel.Show();
  7. for (int i = 0; i < 100; i++)
  8. threadLabel.Text = i.ToString()+" % completed";
  9. }
  10. static ThreadStart ts = new ThreadStart(percentage);
  11. Thread oThread = new Thread(ts);
  12. .....
  13. oThread.Start();
  14. playAnimation();
  15. oThread.Abort();
However the label doesn't show up, but I can see the text property changes when using debugging or Messageboxes..
Can anybody give me a clue where is the problem? Or at least the correct way to access main form labels from another thread..
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Update label text from another thread

 
0
  #2
Mar 22nd, 2009
I sugest you take a different tact.

What I typically do for managing thread messages is I create a simple EventArgs class to hold the message information, a Delegate to represent the main thread method that will use the information, and an eventhandler in the main thread that can be assigned to an event in the thread.

So to start, build a simple EventArgs class
  1. public class ProgressEventArgs : EventArgs
  2. {
  3. private int _maxValue = 100;
  4. private int _value = 0;
  5. private string _text = string.Empty;
  6.  
  7. public int MaxValue
  8. {
  9. get { return _maxValue; }
  10. }
  11. public int Value
  12. {
  13. get { return _value; }
  14. }
  15. public string Text
  16. {
  17. get { return _text; }
  18. }
  19. public ProgressEventArgs(string text, int value, int maxValue)
  20. {
  21. _text = text;
  22. _value = value;
  23. _maxValue = maxValue;
  24. }
  25. public override string ToString()
  26. {
  27. return _text;
  28. }
  29. }

Now create a public delegate. Typically I have a constants.cs file where I create all of my constants, and then outside of the constants class, but in the same name space, I create my delegates like this:

  1. namespace MyNameSpace
  2. {
  3. public delegate void ProgressEvent(object sender, ProgressEventArgs e);
  4. public class Constants
  5. {
  6. }
  7. }

OR you can just add the delegate to the main form.

Next you want to create the method that will receive the event, and process the information (in the main form)
  1.  
  2. public void SetProgressEvent(object sender, ProgressEventArgs e)
  3. {
  4. if( label1.InvokeRequired )
  5. {
  6. ProgressEvent d = new ProgressEvent(SetProgressEvent);
  7. this.Invoke(d, new object[] { sender, e });
  8. }
  9. else
  10. {
  11. progressBar1.Maximum = e.MaxValue;
  12. progressBar1.Value = e.Value;
  13. label1.Text = e.Text;
  14. }
  15. }

Okay, now you want to assign the event to this method. You can do this in the main constructor of your form if this is also where your thread is living. If the thread is in a sperate file or class, then you will declare the event in that class, and attach it from the main form.
  1. onProgressEvent +=new ProgressEvent(SetProgressEvent);

Now inside the thread, you can check to see if the event handler is assigned, and if so send the data to it.
  1. // INSIDE THE THREAD
  2. if(onProgressEvent != null)
  3. {
  4. for(int i=1;i<=100;i++)
  5. {
  6. onProgressEvent(this, new ProgressEventArgs(
  7. i.ToString()+" % completed"
  8. , i
  9. , 100));
  10. System.Threading.Thread.Sleep(20);
  11. }
  12. }

The key to inter thread communication when a Component is involved (or any non thread safe object) is to perform the Invoke as you can see in the SetProgressEvent method.

Hope this help,
// Jerry
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 23
Reputation: Wiizl is an unknown quantity at this point 
Solved Threads: 0
Wiizl Wiizl is offline Offline
Newbie Poster

Re: Update label text from another thread

 
0
  #3
Mar 22nd, 2009
wow thanks for such detailed information!
just to clear things up- that last part should be inside the void method that is passed to threadStart and later to thread, right?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Update label text from another thread

 
1
  #4
Mar 22nd, 2009
Yes, the for--loop is inside the thread method.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 3432 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC