gpt4 book ai didi

C# Windows 窗体线程 : My form seems not to run as own thread

转载 作者:太空宇宙 更新时间:2023-11-03 18:40:05 25 4
gpt4 key购买 nike

我有一个 Windows 窗体。单击“确定”时,将完成一些需要一些时间的工作。在此过程中,表单的所有控件都被禁用,因此用户无法执行某些操作。除了一个按钮,中止。该按钮仍可用于中止流程任务。我开始一个新线程,很好。问题是当单击 ob abort 时,事件在线程完成后被调用。

这是启动线程的代码(在我的 buttonOk_Click 事件方法中)(我在执行某些数据库操作时使用优先级来假装蜜蜂线程中止):

t1 = new System.Threading.Thread(processing);
t1.Priority = System.Threading.ThreadPriority.Normal;
t1.Start();

这是我捕获中止事件但在线程工作期间未访问的代码。 (使用 ButtonAbort_Click 方法)

while (t1.Priority == System.Threading.ThreadPriority.Highest)
{
System.Threading.Thread.Sleep(1000);
}
try
{
lock (t1)
{
if (t1.ThreadState != System.Threading.ThreadState.Aborted)
{
//abort Thread
t1.Abort();
//wait that lock is not released to early
while (t1.ThreadState != System.Threading.ThreadState.Aborted) { }
}
}
}

希望你能帮助我。谢谢。

最佳答案

关于C# Windows 窗体线程 : My form seems not to run as own thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9886570/

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