Dynamic Thread and Label creation
I have a code for new thread in a button new thread is created whenever i hit that button.I want some way so that i can have something on form that contains the progress of all the threads that are created something like list box containing label that show percent done
var t开发者_开发技巧 = new Thread(() =>
{
for(int i=0;i<1000;i++)
{
x++;
}
});
t.SetApartmentState(ApartmentState.STA);
t.Name = projectName;
t.Start();
Sorry if it sounds silly
First of all consider using a Task-based approach (only if you're working with .NET 3.5 or above).
Anyway, when you create a new thread you could add a new label into a ListBox
and update its content as the progress of the work goes on.
For example:
void ButtonClick(object sender, RoutedEvent e)
{
Label label = new Label();
listBox1.Items.Add(label);
Task.Factory.StartNew(() =>
{
for (int i = 0; i < 100; i++)
{
label.Content = (i + 1).ToString() + "%";
}
});
}
Obviously fix cross-thread calls (with Dispatcher.Invoke/Dispatcher.BeginInvoke in WPF, or with label.Invoke in WinForms).
If you keep count of how many threads you launched, and start each off with an index, then the thread could update a progress value at that index.
精彩评论