如何有效地杀死 C# 中的线程? [英] How to kill a thread in C# effectively?

查看:37
本文介绍了如何有效地杀死 C# 中的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是尝试击败 , 老实说.我已经阅读了关于线程终止的所有建议,但是,请考虑代码.它执行以下操作:

I am not trying to beat a dead horse, honestly. And I've read all the advice on thread killing, however, please consider the code. It does the following:

  1. 它启动一个线程(通过StartThread方法)
  2. 它调用数据库查找 ServiceBroker 队列中的任何内容.注意 WAITFOR 命令 - 这意味着它会一直坐在那里直到队列中有东西.所有这些都在 MonitorQueue 方法中.
  3. 杀死线程.我试过 .Interrupt - 它似乎什么也没做.然后我尝试了 .Abort,它不应该被使用,但即使那样也没有任何作用.

  1. It starts a thread (via StartThread method)
  2. It calls the database looking for anything in the ServiceBroker queue. Note the WAITFOR command - it means that it will sit there until there is something in the queue. All this in MonitorQueue method.
  3. Kill the thread. I tried .Interrupt - it seems to do absolutely nothing. Then I tried .Abort, which should never be used, but even that did nothing.

Thread thxMonitor = new Thread(MonitorQueue);
void StartThread() {
    thxMonitor.Start();
}

void MonitorQueue(object obj) {
    var conn = new SqlConnection(connString);
    conn.Open();
    var cmd = conn.CreateCommand();
    cmd.CommandTimeout = 0; // forever and ever
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "WAITFOR (RECEIVE CONVERT(int, message_body) AS Message FROM SBQ)";

    var dataTable = new DataTable();
    var da = new SqlDataAdapter(command);

    da.Fill(dataTable);
    da.Dispose();
}

void KillThreadByAnyMeansNecessary() {
    thxMonitor.Interrupt();
    thxMonitor.Abort();
}

真的可以杀死一个线程吗?

Is it actually possible to kill a thread?

推荐答案

设置一个 Abort 标志来告诉线程需要终止.将虚拟记录附加到 ServiceBroker 队列.WAITFOR 然后返回.然后线程检查它的中止"标志,发现它被设置,从队列中删除虚拟记录并退出.

Set an Abort flag to tell the thread is needs to terminate. Append a dummy record to the ServiceBroker queue. The WAITFOR then returns. The thread then checks its 'Abort' flag and, finding it set, deletes the dummy record from the queue and exits.

另一种变体是向 ServiceBroker 监视的表的规范添加真实"毒丸记录 - 非法记录号等.这将完全避免以任何直接方式接触线程 - 总是一件好事:) 这可能更复杂,特别是如果每​​个工作线程都期望在实际终止时通知,但如果工作线程仍然有效,ServiceBroker 和 DB 都在不同的盒子上.我将其添加为编辑,因为考虑了更多,它似乎更灵活,毕竟,如果线程通常仅通过通信进行通信.DB,为什么不只用 DB 关闭它们呢?没有 Abort(),没有 Interrupt(),希望没有产生锁定的 Join().

Another variant would be to add a 'real' poison-pill record to the specification for the table monitored by the ServiceBroker - an illegal record-number, or the like. That would avoid touching the thread/s at all in any direct manner - always a good thing:) This might be more complex, especially if each work thread is expeceted to notify upon actual termination, but would still be effective if the work threads, ServiceBroker and DB were all on different boxes. I added this as an edit because, having thought a bit more about it, it seems more flexible, after all, if the threads normally only communicate via. the DB, why not shut them down with only the DB? No Abort(), no Interrupt() and, hopefully, no lockup-generating Join().

这篇关于如何有效地杀死 C# 中的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆