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

Recommended Answers

All 10 Replies

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

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?

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

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

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

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

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

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

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

I get to onProgressEvent +=new ProgressEvent(SetProgressEvent); and it tells me "The name 'onProgressEvent' does not exist in the current context."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.