gpt4 book ai didi

c# - 进度条和线程

转载 作者:行者123 更新时间:2023-11-30 22:17:44 25 4
gpt4 key购买 nike

我有这个进度条类(测试线程)

public class ProgressBarUpdate
{
//Add getters and setters
static MainGUI theForm = (MainGUI)Application.OpenForms[0];
ProgressBar pBarCur = theForm.pBarCur; //Yes, accessing public for now
bool updateCur = false;
bool stopCur = false;
bool showMax = false;
public ProgressBarUpdate()
{

}
public void resetCur()
{
pBarCur.Value = 0;
}
public void DoCurUpdate()
{
while (!stopCur)
{
if (pBarCur.Value < (pBarCur.Maximum / 10) * 9)
pBarCur.PerformStep();
if (showMax)
{
pBarCur.Value = pBarCur.Maximum;
showMax = false;
}
}

}
public void StopCur()
{
stopCur = true;
}
public void UpdateCur()
{
updateCur = true;
}
public void UpdateToMax()
{
showMax = true;
}

然后我在不同的 A 类中调用所有这些以从那里更新 GUI:

ProgressBarUpdate updateBar = new ProgressBarUpdate();

Thread currentProgressUpdater = new Thread(new ThreadStart(updateBar.DoCurUpdate));

try
{
currentProgressUpdater.Start();

currentProgressUpdater.Join();
}
catch (Exception)
{

}

在我运行它之后,我会看到我的应用程序已停止响应(立即)的对话框,然后它要求我关闭。我没有正确实现线程吗?还是我错过了一步?

最佳答案

您的问题是对 currentProgressUpdater.Join(); 的调用。您正在阻塞 UI 线程。

创建新线程的全部意义在于允许 UI 线程继续处理 UI 事件。你不会让它那样做。启动一个线程然后立即加入它与仅执行行中的代码并没有什么不同。

您还从在新线程中运行的方法访问控件。那行不通的。只能从 UI 线程访问 UI 控件。

关于c# - 进度条和线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16591012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com