我有一个Windows窗体.单击"确定"时,将完成一些工作,这需要一些时间.在此过程中,窗体的所有控件都被禁用,因此用户无法执行某些操作.除了一个按钮,中止.此按钮仍可用于中止过程任务.我开始一个新线程,很好.问题是当单击ob abort时,在线程结束后调用该事件.
下面是代码来启动线程(在我的buttonOk_Click Eventmethod中)(我正在使用优先级来假装在我做一些数据库操作时中止线程):
t1 = new System.Threading.Thread(processing);
t1.Priority = System.Threading.ThreadPriority.Normal;
t1.Start();
Run Code Online (Sandbox Code Playgroud)
下面是我捕获中止事件的代码,但是在线程工作期间没有访问它.(使用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) { }
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望您能够帮助我.谢谢.
| 归档时间: |
|
| 查看次数: |
1292 次 |
| 最近记录: |