Shi*_*ano 2 c# multithreading kill
我有一个线程打开一个MyMessageAlert类型的形式.此表单是一个弹出窗口,在我调用它时打开.它有一个定时器,在30秒后调用CloseWindow()方法.
m_messagAlert = new MyMessageAlert();
ParameterizedThreadStart thStart = new ParameterizedThreadStart(m_messagAlert.setMessage);
Thread thread = new Thread(thStart);
thread.Start(strMessage); //at this point, the MyMessageAlert form is opened.
Run Code Online (Sandbox Code Playgroud)
我已经定义了一个类型线程列表:
public List<Thread> m_listThread;
Run Code Online (Sandbox Code Playgroud)
每次我创建一个线程,我都将它添加到我的列表中:
m_listThread.Add(thread);
Run Code Online (Sandbox Code Playgroud)
当我关闭应用程序时,我希望打开的MyMessageAlert类型的形式将立即关闭(不等待30秒).问题是我无法阻止它!
我尝试使用Abort()函数通过循环遍历列表来杀死它:
foreach (Thread thread in m_listThread)
{
if (thread.IsAlive)
thread.Abort();
}
Run Code Online (Sandbox Code Playgroud)
但它没有帮助.
jb_*_*jb_ 11
也许你想使用Background-Threads而不是Foreground-Threads.您的应用程序以至少一个Foreground-Thread(主进程线程)开头.如果直接使用Thread-class创建新的Threads(例如新的Thread(...)),这将是一个前台线程.
当至少一个前台线程仍在运行时,不会卸载AppDomain.要避免此行为,您可以创建后台线程.通常,您使用ThreadPool类中的后台线程,或者IsBackground直接在线程对象上设置-Property.
编辑:MSDN的简短说明.
| 归档时间: |
|
| 查看次数: |
5004 次 |
| 最近记录: |