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

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

问题描述

我不试图到的beat 一个<一个href="http://stackoverflow.com/questions/2251964/c-sharp-thread-termination-and-thread-abort">dead ,的诚实。我读过关于线程查杀所有的建议,但是,请考虑code。它执行以下操作:

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?

推荐答案

设置中止标志告诉线程需要终止。追加一个伪记录的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监控 - 一个非法的记录号码等。这将避免触摸线程/ s的所有任何直接的方式 - 总是一件好事:)这可能是比较复杂的,尤其是当每个工作线程expeceted在实际的终止通知,但仍然是有效的,如果工作线程, ServiceBroker和DB都在不同的盒子。我说这是一个编辑,因为其认为有点多了解一些,似乎更灵活,毕竟,如果线程通常只能通过通信。数据库,为什么不只是DB关闭它们?没有中止(),无中断(),并希望,不死机生成加入()。

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天全站免登陆