| | |
Update label text from another thread
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2007
Posts: 23
Reputation:
Solved Threads: 0
I'm imitating progressbar value change like:
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.
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..
C# Syntax (Toggle Plain Text)
private void playAnimation() { for(int i=1;i<=100;i++) { this.progressBar1.Value = i; System.Threading.Thread.Sleep(20); percent = i; } }
C# Syntax (Toggle Plain Text)
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();
Can anybody give me a clue where is the problem? Or at least the correct way to access main form labels from another thread..
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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
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:
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)
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.
Now inside the thread, you can check to see if the event handler is assigned, and if so send the data to it.
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
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
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
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)
C# Syntax (Toggle Plain Text)
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.
C# Syntax (Toggle Plain Text)
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.
C# Syntax (Toggle Plain Text)
// 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
![]() |
Similar Threads
- Starting wxPython (GUI code) (Python)
- Threading Module confusion (Python)
- Problem using Windows commands in a Java Networking application (Java)
- Show Progress in % problem (C++)
- multithreading exception (C#)
- Please help! I am going insane! (VB.NET)
- Simple Hangman Game (Visual Basic 4 / 5 / 6)
- Frozen forms on another thread (C#)
- Getting my data back from my JList (Java)
- Dots-and-Boxes game - RFC (Python)
Other Threads in the C# Forum
- Previous Thread: Problem with updating the database tables
- Next Thread: Passing values with textbox
Views: 3432 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





