我有一个函数要处理的时间会比较长,所以目前考虑用一个进度条来显示正在运行该函数,直到该函数处理完进度条停止。public void Main
{
achieve = false;
Thread t = new Thread(new ThreadStart(SetProgress));
t.Start();
ArrangeClass() ////此函数运行时间长
achieve = true;
MessageBox.Show("运行完毕");
}bool achieve = false;
private int step =1;
public void SetProgress()
{
while (!achieve)
{
if(progressBar1.Position == 100) step = -1;
else if (progressBar1.Position == 0) step = 1;
progressBar1.Increment(step);
//progressBar1.Update();
Thread.Sleep(20);
}
}这样做不能达到效果。。边运行函数,边显示进度条。
请问这段代码该做何修改?
{
achieve = false;
Thread t = new Thread(new ThreadStart(SetProgress));
t.Start();
ArrangeClass() ////此函数运行时间长
achieve = true;
MessageBox.Show("运行完毕");
}bool achieve = false;
private int step =1;
public void SetProgress()
{
while (!achieve)
{
if(progressBar1.Position == 100) step = -1;
else if (progressBar1.Position == 0) step = 1;
progressBar1.Increment(step);
//progressBar1.Update();
Thread.Sleep(20);
}
}这样做不能达到效果。。边运行函数,边显示进度条。
请问这段代码该做何修改?
能说详细点吗?
或是说说我的程序该如何修改?
Windows的搜索。
,因为它们是在主线程中声明的,不能在另一个线程中使用
.asp这个例子很经典。我就是模仿它开发了一个短信收发的程序。
Thread timerThread = new Thread(new ThreadStart(ThreadProc));
timerThread.IsBackground = true;
arrangeStudent.ArrangeClass(); ///长时间运行
timerThread.Suspend();
timerThread.Start();
}public void ThreadProc()
{
try
{
MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
while (true)
{
this.BeginInvoke(mi);
Thread.Sleep(500) ;
}
}
catch
{}
}private int step =1;
private void UpdateProgress()
{
if(bar1.Position == 100) step = -1; //bar1为进度条progressBar
else if (bar1.Position == 0) step = 1;
bar1.Increment(step);
}各位请帮我看下这些代码。
现在线程好像有同时进行,但是progressBar却没有显示。请问这是为何?