我有一个Windows窗体。点击“确定”后,将完成一些需要一些时间的工作。在此过程中,表单的所有控件都被禁用,因此用户无法执行某些操作。除了一个按钮,中止。该按钮仍可用于中止处理任务。 我开始一个新的主题,很好。问题是,当单击ob中止时,在线程完成后调用该事件。C#窗体窗体线程:我的窗体似乎不作为自己的线程运行
继承人,他的代码来启动线程(我buttonOk_Click Eventmethod内)(我使用的优先级假装的,而我做一些数据库操作beeing中止的线程):
t1 = new System.Threading.Thread(processing);
t1.Priority = System.Threading.ThreadPriority.Normal;
t1.Start();
继承人的代码,其中我抓住了中止事件,但在线程正在工作期间没有被访问。 (Withing ButtonAbort_Click-Method)
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) { }
}
}
}
希望你能帮助我。谢谢。
这是你在那里的一些非常奇怪的代码。什么是优先检查? – Tudor 2012-03-27 09:32:58
不要做thread.abort()它会搞砸了。使用一个标志并在线程中检查这个标志,让它自行停止。 – RvdK 2012-03-27 10:21:48