多线程和进度 [英] Multithreading and ProgressBar
问题描述
需要作出多线程应用程序,它执行一些活动和显示的所有工作进度的进展。这里是代码。只有线程名为话题 - 0使所有的工作。我需要所有的线程之间分配工作。
公共部分Form1类:表格
{
常量浮动maxWorkedValue = 10;
const int的threadsCount = 5;
浮动totalProgress;
INT threadSleepTime = 1;
对象更衣室=新的对象();
名单,LT;主题>线程=新的List<螺纹>();
公共Form1中()
{
的InitializeComponent();
prBar.Maximum =(INT)maxWorkedValue;
prBar.Step = 1;
的for(int i = 0; I< threadsCount;我++)
{
threads.Add(新线(BeginCalculate){名称=主题 - + I。的ToString()});
线程[I]。开始();
}
}
无效BeginCalculate()
{
为(INT I = 0; I&下; maxWorkedValue;我++)
{
锁(锁定装置)
{
浮子currentProgress = CalculateLongTask(ⅰ);
更新((currentProgress / maxWorkedValue)* 100,Thread.CurrentThread.Name);
Thread.sleep代码(threadSleepTime);
}
}
}
浮动CalculateLongTask(int值)
{
的for(int i = 0; I< ;价值;我++)
Thread.sleep代码(threadSleepTime);
返回totalProgress ++;
}
无效更新(浮点我,串threadName)
{
如果(InvokeRequired)
{
的BeginInvoke(新动作<浮动,串>(更新),新的对象[] {我,threadName});
的回报;
}
label1.Text = threadName;
prBar.Value =(INT)I;
}
}
UPDATE2:
我'已经更新上面的代码。但是它不能解决问题。如何为我不同的线程之间的变量同步?一个例外(progressbar.Max)被抛出。为什么
的for(int i = 0; I< maxWorkedValue;我+ +)?
{
锁(柜)
{
浮动currentProgress = CalculateLongTask(I)
更新((currentProgress / maxWorkedValue)* 100,Thread.CurrentThread.Name);
Thread.sleep代码(15);
}
}
编辑
如果我理解正确的话,你要执行一个循环(即的for(int i = 0; I< maxWorkedValue;我++)
),同时在许多线程。
这是非常容易使用的并行扩展来完成
1 。SUP>
下面是修改后的代码:
公共部分Form1类:表格
{
常量浮动maxWorkedValue = 100;
const int的THREADCOUNT = 5;
浮动maxWorkedValue2 = 1;
线程线程;
公共Form1中()
{
的InitializeComponent();
prBar.Maximum =(INT)maxWorkedValue;
prBar.Step = 1;
线程=新主题(BeginCalculate){名称=父线程};
thread.Start();
}
无效BeginCalculate()
{
的Parallel.For(0,(INT)maxWorkedValue,
(1)=>
{
浮动currentProgress = CalculateLongTask(I);
//这里,我们是在一个线程孩子
//(可能)父亲线程让我们用ManagedThreadId代替名称...
更新((currentProgress / maxWorkedValue)* 100,Thread.CurrentThread.ManagedThreadId.ToString());
Thread.sleep代码(2);
返回;
});
}
浮动CalculateLongTask(int值)
{
的for(int i = 0; I<值; I ++)
Thread.sleep代码(15);
返回maxWorkedValue2 ++;
}
无效更新(浮点我,串threadName)
{
如果(InvokeRequired)
{
的BeginInvoke(新动作<浮动,串>(更新),新的对象[] {我,threadName});
的回报;
}
label1.Text = threadName;
prBar.Value = Math.Min((int)的I,100);
}
}
1 结果
。如果你使用.NET 4.0并行扩展自带的框架。结果
如果'重新使用.net 3.5,看这个的 Q&安培; A 知道如何使用它
Need to make an multithreading application, which performs some activities and shows progress of all work in ProgressBar. Here is the code. Only thread named "thread - 0" makes all work. I need to distribute work between all threads.
public partial class Form1 : Form
{
const float maxWorkedValue = 10;
const int threadsCount = 5;
float totalProgress;
int threadSleepTime = 1;
object locker = new object();
List<Thread> threads = new List<Thread>();
public Form1()
{
InitializeComponent();
prBar.Maximum = (int)maxWorkedValue;
prBar.Step = 1;
for (int i = 0; i < threadsCount; i++)
{
threads.Add(new Thread(BeginCalculate) { Name = "Thread - " + i.ToString() });
threads[i].Start();
}
}
void BeginCalculate()
{
for (int i = 0; i < maxWorkedValue; i++)
{
lock (locker)
{
float currentProgress = CalculateLongTask(i);
Update((currentProgress / maxWorkedValue) * 100, Thread.CurrentThread.Name);
Thread.Sleep(threadSleepTime);
}
}
}
float CalculateLongTask(int value)
{
for (int i=0; i<value;i++)
Thread.Sleep(threadSleepTime);
return totalProgress++;
}
void Update(float i, string threadName)
{
if (InvokeRequired)
{
BeginInvoke(new Action<float, string>(Update), new object[] { i, threadName });
return;
}
label1.Text = threadName;
prBar.Value = (int)i;
}
}
UPDATE2: I've updated the code above. But it not resolved the problem. How to synchronize the variable "i" between different threads? An exception (progressbar.Max) is thrown. Why?
for (int i = 0; i < maxWorkedValue; i++)
{
lock (locker)
{
float currentProgress = CalculateLongTask(i);
Update((currentProgress / maxWorkedValue) * 100, Thread.CurrentThread.Name);
Thread.Sleep(15);
}
}
EDIT:
If I understood you correctly you want to execute a loop (i.e.for (int i = 0; i < maxWorkedValue; i++)
) concurrently among many threads.
This is extremely easier to accomplish using the Parallel extensions 1.
Here's your modified code:
public partial class Form1 : Form
{
const float maxWorkedValue = 100;
const int threadCount = 5;
float maxWorkedValue2 = 1;
Thread thread;
public Form1()
{
InitializeComponent();
prBar.Maximum = (int)maxWorkedValue;
prBar.Step = 1;
thread = new Thread(BeginCalculate) { Name = "Father thread" };
thread.Start();
}
void BeginCalculate()
{
Parallel.For(0, (int)maxWorkedValue,
(i) =>
{
float currentProgress = CalculateLongTask(i);
// here we are (probably) in a thread child
//of "Father thread" so let's use ManagedThreadId instead of the name...
Update((currentProgress / maxWorkedValue) * 100, Thread.CurrentThread.ManagedThreadId.ToString());
Thread.Sleep(2);
return;
});
}
float CalculateLongTask(int value)
{
for (int i = 0; i < value; i++)
Thread.Sleep(15);
return maxWorkedValue2++;
}
void Update(float i, string threadName)
{
if (InvokeRequired)
{
BeginInvoke(new Action<float, string>(Update), new object[] { i, threadName });
return;
}
label1.Text = threadName;
prBar.Value = Math.Min((int)i, 100);
}
}
1
If you're using .NET 4.0 Parallel extensions comes with the framework.
If you're using .NET 3.5, look at this Q&A to know how to use it.
这篇关于多线程和进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!