1,105,328 Community Members

Update label text from another thread

Member Avatar
Wiizl
Light Poster
25 posts since Jan 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm imitating progressbar value change like:

private void playAnimation()
        {
            for(int i=1;i<=100;i++)
            {
                this.progressBar1.Value = i;
                System.Threading.Thread.Sleep(20);
                percent = i;
            }
        }

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.

private static void percentage()
        {
            Label threadLabel = new Label();
            Point p = new Point(418,290);
            threadLabel.Location = p;
            threadLabel.Show();
            for (int i = 0; i < 100; i++)
            threadLabel.Text = i.ToString()+" % completed";
        }
        static ThreadStart ts = new ThreadStart(percentage);
        Thread oThread = new Thread(ts);
.....
oThread.Start();
playAnimation();
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..

Member Avatar
JerryShaw
Posting Pro in Training
467 posts since Nov 2006
Reputation Points: 46 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 0 [?]
 
0
 

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

public class ProgressEventArgs : EventArgs
    {
        private int _maxValue = 100;
        private int _value = 0;
        private string _text = string.Empty;

        public int MaxValue
        {
            get { return _maxValue; }
        }
        public int Value
        {
            get { return _value; }
        }
        public string Text
        {
            get { return _text; }
        }
        public ProgressEventArgs(string text, int value, int maxValue)
        {
            _text = text;
            _value = value;
            _maxValue = maxValue;
        }
        public override string ToString()
        {
            return _text;
        }
    }

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:

namespace MyNameSpace
{
    public delegate void ProgressEvent(object sender, ProgressEventArgs e);
    public class Constants
   {
   }
}

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)

public void SetProgressEvent(object sender, ProgressEventArgs e)
        {
            if( label1.InvokeRequired )
            {
                ProgressEvent d = new ProgressEvent(SetProgressEvent);
               this.Invoke(d, new object[] { sender, e });
            }
            else
            {
                progressBar1.Maximum = e.MaxValue;
                progressBar1.Value = e.Value;
                label1.Text = e.Text;
            }
        }

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.

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.

// INSIDE THE THREAD
    if(onProgressEvent != null)
    { 
            for(int i=1;i<=100;i++)
            {
               onProgressEvent(this, new ProgressEventArgs(
                       i.ToString()+" % completed"
                      , i
                      , 100));
                System.Threading.Thread.Sleep(20);
            }
    }

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

Member Avatar
Wiizl
Light Poster
25 posts since Jan 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
JerryShaw
Posting Pro in Training
467 posts since Nov 2006
Reputation Points: 46 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 0 [?]
 
1
 

Yes, the for--loop is inside the thread method.

Question Answered as of 5 Years Ago by JerryShaw
Member Avatar
aishev
Newbie Poster
2 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Can you guide me to use the same method, but not in windows form? just to update the label text. Thanks.

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

This is another version of updating Label`s text over the thread:

delegate void LabelDelegate(string message);
        public Form1()
        {
            InitializeComponent();         
        }

        private void UpdatingLabel(string msg)
        {
            if (this.label1.InvokeRequired)
                this.label1.Invoke(new LabelDelegate(UpdatingLabel), new object[] { msg });
            else
                this.label1.Text = msg;
        }

Its faster and easier...

Member Avatar
crishjeny
Junior Poster in Training
98 posts since Nov 2010
Reputation Points: -10 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
-1
 

I'm imitating progressbar value change like:

private void playAnimation()
        {
            for(int i=1;i<=100;i++)
            {
                this.progressBar1.Value = i;
                System.Threading.Thread.Sleep(20);
                percent = i;
            }
        }

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.

private static void percentage()
        {
            Label threadLabel = new Label();
            Point p = new Point(418,290);
            threadLabel.Location = p;
            threadLabel.Show();
            for (int i = 0; i < 100; i++)
            threadLabel.Text = i.ToString()+" % completed";
        }
        static ThreadStart ts = new ThreadStart(percentage);
        Thread oThread = new Thread(ts);
.....
oThread.Start();
playAnimation();
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..

hi

what is the problem you are facing in this threading ? please write your error while threading generate

Member Avatar
aishev
Newbie Poster
2 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I want too update label.text, but in aspx webforom, not in windows form. Can anyone get any suggestions?thanks.

Member Avatar
JerryShaw
Posting Pro in Training
467 posts since Nov 2006
Reputation Points: 46 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 0 [?]
 
0
 

This is another version of updating Label`s text over the thread:

delegate void LabelDelegate(string message);
        public Form1()
        {
            InitializeComponent();         
        }

        private void UpdatingLabel(string msg)
        {
            if (this.label1.InvokeRequired)
                this.label1.Invoke(new LabelDelegate(UpdatingLabel), new object[] { msg });
            else
                this.label1.Text = msg;
        }

Its faster and easier...

Mitja, inline delegate assignment is not any faster in execution. As for easier, only saves the typing of a delegate type and a variable name. As for maintenance and readability, I prefer the longer form especially is using non standard EventArg handlers.
JMO

Member Avatar
rasoulian
Newbie Poster
1 post since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(Go);
th.Start(textBox1.Text);
}

private void Go(object obj)
{
button1.Invoke(new Action(() => button1.Text = obj as string));
}

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article