我创建了一个 C# windows 应用程序,其中插入了进度条。
当一个按钮被点击时,进度条应该出现,然后它应该开始这个过程大约 2 到 3 秒,当过程条完成时它应该被隐藏。
我已经使用此代码解决了这个问题,但它不起作用。
在进度条运行时,标签框应该是“正在生成... 45%”,完成标签框后应该是“已生成 100%..”,但是当我插入标签时,它会显示一些错误。
这是单击“生成”按钮之前的图片。
在处理中我应该得到这样的..
在最终进程 id 应该是这样的并且进度条应该隐藏..
ProgressBar1.Visible = true;
if (isProcessRunning)
{
MessageBox.Show("A process is already running.");
return;
}
Thread backgroundThread = new Thread(
new ThreadStart(() =>
{
isProcessRunning = true;
for (int n = 0; n < 100; n++)
{
Thread.Sleep(1);
progressBar1.BeginInvoke(new Action(() => progressBar1.Value = n));
}
MessageBox.Show("Generated!!!");
if (progressBar1.InvokeRequired)
progressBar1.BeginInvoke(new Action(() => progressBar1.Value = 0));
isProcessRunning = false;
}
));
// Start the background process thread
backgroundThread.Start();