多线程进度条的问题,头疼死了,以下是源代码,哪位高人给看看啥问题
private void button1_Click(object sender, EventArgs e)
{ info _info=new info();
_info.total=20;
ManualResetEvent[] _ManualEvents = new ManualResetEvent[_info.total];
for (int i = 0; i < _info.total; i++)
{
_ManualEvents[i] = new ManualResetEvent(false);
_info.current = i;
_info.MRevent = _ManualEvents[i];
ThreadPool.QueueUserWorkItem(new WaitCallback(dosomething), _info);
}
WaitHandle.WaitAll(_ManualEvents);//有这句程序就死掉了,没有的话进度条正常************************
} public struct info
{
public int total;
public int current;
public ManualResetEvent MRevent;
}
private void ShowProgress(int nTotal, int nCurrent)
{
if (this.InvokeRequired)
{
ShowProgressDelegate oShowProgress = new ShowProgressDelegate(ShowProgress);
this.Invoke(oShowProgress, new object[] { nTotal, nCurrent });
}
else
{
this.progressBar1.Maximum = nTotal;
this.progressBar1.Value = nCurrent;
}
} public void dosomething(object o)
{
info _info = (info)o;
Thread.Sleep(100);
_info.current++;
ShowProgress(_info.total, _info.current);
ManualResetEvent e = _info.MRevent;
e.Set();
}多线程进度条ThreadPoolWaitHandle.WaitAll
private void button1_Click(object sender, EventArgs e)
{ info _info=new info();
_info.total=20;
ManualResetEvent[] _ManualEvents = new ManualResetEvent[_info.total];
for (int i = 0; i < _info.total; i++)
{
_ManualEvents[i] = new ManualResetEvent(false);
_info.current = i;
_info.MRevent = _ManualEvents[i];
ThreadPool.QueueUserWorkItem(new WaitCallback(dosomething), _info);
}
WaitHandle.WaitAll(_ManualEvents);//有这句程序就死掉了,没有的话进度条正常************************
} public struct info
{
public int total;
public int current;
public ManualResetEvent MRevent;
}
private void ShowProgress(int nTotal, int nCurrent)
{
if (this.InvokeRequired)
{
ShowProgressDelegate oShowProgress = new ShowProgressDelegate(ShowProgress);
this.Invoke(oShowProgress, new object[] { nTotal, nCurrent });
}
else
{
this.progressBar1.Maximum = nTotal;
this.progressBar1.Value = nCurrent;
}
} public void dosomething(object o)
{
info _info = (info)o;
Thread.Sleep(100);
_info.current++;
ShowProgress(_info.total, _info.current);
ManualResetEvent e = _info.MRevent;
e.Set();
}多线程进度条ThreadPoolWaitHandle.WaitAll
解决方案 »
- C# 数据库问题
- 为什么文本包含“一夜情”就无法保存?
- 麻烦一下大牛们~~关于登陆界面的判断的问题。
- C#DataGridView分页功能
- 关于设定ComboBox.SelectedItem的奇怪问题
- 如何在加载主窗体之前,通过异步启动另一个窗体提示正在链接数据库
- 小弟刚转到C#,请教有关线程的问题?怎样在登陆窗体上一边连SQL,一边显示一个动态进度条?
- 阅读器关闭时 read data 的尝试无效。
- 如何使用用户开发的服务器控件?
- 为什么下载的visual studio2017里没有onmousedouwn语句
- 如何用c#记录特定文档打开时间及次数
- 请教:虚方法在继承时如何处理不需要的部分最好?
里面的代码,再用一个线程包起来执行,就不会死了.