在线程中运行进度条 [英] Run a progressbar in a thread
问题描述
您好,我想在一个帖子中运行一个进度条。我做了这个代码,但它没有用。
你能不能请我帮忙。谢谢
< span class =code-keyword> private void button2_Click( object sender,EventArgs e )
{
Thread th = new Thread(st);
th.Start();
}
void st()
{
for ( int i = 0 ; i < 101 ; i ++)
{
progressBar1.Value = i;
Thread.Sleep( 10 );
}
}
i得到此错误
Control.Invoke必须用于与在单独的线程上创建的控件进行交互。
我是什么尝试过:
private void button2_Click(object sender,EventArgs e)
{
线程th =新线程(st);
th.Start();
}
void st()
{
for(int i = 0; i< 100; i ++)
{
progressBar1.Value = 100;
}
}
您需要做的就是将ProgressBar
的更新编组回UI线程。这很简单:
private void button2_Click( object sender,EventArgs e)
{
Thread th = new 螺纹(ST);
th.Start();
}
void st()
{
函数更新=(i)= > {progressBar1.Value = i; };
object [] upi = new object [ 1 ];
for ( int i = 0 ; i < 101 ; i ++)
{
upi [< span class =code-digit> 0 ] = i;
progressBar1.Invoke(update,upi);
Thread.Sleep( 10 );
}
}
这将启动已启动的线程并更新ProgressBar
每10毫秒,而应用程序的其余部分运行不受阻碍。 (您可能需要更长的延迟来验证这是否正在运行而不会阻止UI线程......)
请参阅MSDN:Control.Invoke方法(委托,对象[])(System.Windows.Forms) [ ^ ]
这样做:
私人 委托 void ProgressHandler();
private void DoProgress()
{
for ( int i = 0 ; i < 101 ; i ++)
{
progressBar1.Value = i;
Thread.Sleep( 10 );
}
}
然后:
void st()
{
if (progressBar1.InvokeRequired)
progressBar1.Invoke( new ProgressHandler(DoProgress));
else
DoProgress();
}
然而,这种ProgressBar毫无意义,因为ProgressBass必须在您的应用程序中显示一些进展。
Hi there, i want to run a progressbar in a thread. i did this code but it didn't work
could you helpe me please .thanks
private void button2_Click(object sender, EventArgs e)
{
Thread th = new Thread(st);
th.Start();
}
void st()
{
for (int i = 0; i < 101; i++)
{
progressBar1.Value=i;
Thread.Sleep(10);
}
}
i get this error
Control.Invoke must be used to interact with controls created on a separate thread.
What I have tried:
private void button2_Click(object sender, EventArgs e)
{
Thread th = new Thread(st);
th.Start();
}
void st()
{
for (int i = 0; i < 100; i++)
{
progressBar1.Value = 100;
}
}
All you need to do is to marshall the update of theProgressBar
back to the UI thread. This is very easy:
private void button2_Click(object sender, EventArgs e) { Thread th = new Thread(st); th.Start(); } void st() { Function update = (i) => { progressBar1.Value = i; }; object[] upi = new object[1]; for (int i = 0; i < 101; i++) { upi[0] = i; progressBar1.Invoke(update, upi); Thread.Sleep(10); } }
This will have the started thread running and updating theProgressBar
every 10ms, while the rest of the application runs unimpeded. (You probably want a longer delay to verify that this is running without blocking the UI thread...)
See MSDN: Control.Invoke Method (Delegate, Object[]) (System.Windows.Forms)[^]
Do this:
private delegate void ProgressHandler(); private void DoProgress() { for (int i = 0; i < 101; i++) { progressBar1.Value=i; Thread.Sleep(10); } }
then:
void st() { if (progressBar1.InvokeRequired) progressBar1.Invoke(new ProgressHandler(DoProgress)); else DoProgress(); }
However, this kind of ProgressBar is meaningless because ProgressBass has to show some progress in your application.
这篇关于在线程中运行进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!