杀/中止线程与一个单一的,巨大的任务(如SQL查询)线程、巨大、任务、SQL

2023-09-03 17:29:20 作者:說恏卟侢哭ㄋ

好了,

有在你的循环将退出检查似乎是大量的讨论就到这里怎么可怕可怕Thread.Abort的()的,以及如何使用Thread.Abort的()解决您的code

There seems to be a lot of discussion on here about how terribly awful Thread.Abort() is, and how to work around using Thread.Abort() in your code by adding exit checks in your loop.

这假定您控制如何工作正在分割的方式,是颗粒状足以遍历它。

This assumes that you control how the work is being partitioned in a way that's granular enough to loop over it.

但我怎么做到以下几点:

But how do I do the following:

启动工作线程做一些事情,同时保持用户界面响应。对于所有意图和目的的code的工作线程看起来是这样的:

Launch worker thread to do something while keeping the UI responsive. For all intents and purposes the code for the worker thread looks like this:

public override void Run()
{
    try
    {
        _dataTable = ExecuteSingleGinormousSqlQuery();
    } finally
    {
        // close the connection if it was left open.
    }
}

有取消按钮,以便用户可以中断这个,如果他们觉得无聊的等待。

Have "Cancel" button so that the user can interrupt this if they get bored of waiting.

它似乎并不像有解决这个其他方式比Thread.Abort的()。真的吗?此外,在经验测试,它甚至不看起来Thread.Abort的()实际上杀死线程,直到这些发起查询的呼叫,如

It doesn't seem like there's any other way around this than with Thread.Abort(). Is that true? Furthermore, in empirical tests, it doesn't even seem like Thread.Abort() actually kills the thread until the call that is making the query, e.g.

    new SqlDataAdapter(cmd).Fill( ds );

伤愈复出摆在首位,因此显著否定它的效用。

comes back in the first place, thus significantly negating its utility.

有没有解决的办法吗?最佳做法?

Is there a way around this? Best practices?

推荐答案

您说的没错,它不会立即中止一个线程是忙的互操作调用,这可能是为什么它不会停止,直到数据库后,通话完毕。 (见 http://www.devx.com/$c $ CMAG /条/ 17442/0 /页/ 4 )

You're right that it won't immediately abort a thread that's busy in an interop call, which is probably why it doesn't stop until after the database call is completed. (See http://www.devx.com/codemag/Article/17442/0/page/4)

我想你可以拨打电话异步(见 http://www.devx.com/ DOTNET /条/ 26747 ),但所有的意思是,你不必中止线程。该呼叫继续在后台,不被中止在数据库端。你还不如只使用一个同步调用并让该线程完成,而忽略它的结果。另一个问题是,你不能用一个简单的填充的方法做的工作,如果你想要去的异步。

I suppose you could make the call asynchronous (see http://www.devx.com/dotnet/Article/26747) but all that means is that you don't have to abort the thread. The call continues on in the background and isn't being aborted on the database end. You might as well just use a synchronous call and let the thread complete, ignoring its results. The other issue is that you can't use a simple Fill method to do the work, if you want to go async.