什么'线程被中止'.'在SNIReadSync(SNI_Conn*,SNI_Packet**,Int32)'是什么意思?

use*_*531 12 sql-server asp.net

你好,

当处理大量数据时,我在ASP.Net WebApp(使用SQL-Server 2008)中获得以下异常 - 并且似乎在代码中的随机位置抛出此异常.

这个例外是什么意思?是超时吗?

Thread was being aborted.
   at SNIReadSync(SNI_Conn* , SNI_Packet** , Int32 )
 at SNINativeMethodWrapper.SNIReadSync(SafeHandle pConn, IntPtr& packet, Int32 timeout)
 at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
 at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
 at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
 at System.Data.SqlClient.TdsParserStateObject.ReadByte()
 at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
 at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
 at System.Data.SqlClient.SqlDataReader.get_MetaData()
 at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
 at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
 at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
 at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
 at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
 at System.Data.SqlClient.SqlCommand.Exec
Run Code Online (Sandbox Code Playgroud)

谢谢!

Kei*_*thS 2

99% 的情况下,“线程正在中止”错误是由于在代码中使用 Thread.Abort() 在除灾难性故障之外的任何其他情况下结束进程而引起的。Thread.Abort 是邪恶的,因为它从线程自身的执行代码外部向线程注入异常,因此期望并优雅地处理它是极其困难的,甚至是不可能的。

如果您在另一个线程中运行此代码(顺便说一句,不错的选择;数据库操作是多线程的自然候选者),请勿使用 Thread.Abort() 来尝试控制线程。相反,您应该构建线程的代码来响应您可以触发的某些外部更改,这将导致它正常结束处理。这是一个简单的例子:

public class Foo
{
    public void MainMethod()
    {
        bool cancel = false; //our external flag

        //our worker thread, which is being given a callback method to poll at "safe" times.
        var workerThread = new Thread(()=>AsyncMethod(()=>cancel));
        //start the thread
        workerThread.Start();
        //do some other work that takes less time than the thread
        Thread.Sleep(200)
        //async thread is still going; cancel execution and wait for graceful exit.
        cancel = true;
        workerThread.Join();
    }

    public void AsyncMethod(Func<bool> wasCancelled)
    {
        //Do some repetitive task that takes longer than we're willing to wait
        for(var i=1; i<2000; i++)
        {
            if(wasCancelled()) break; //generally a "safe" place to check
            Thread.Sleep(50); //stand-in for some atomic operation that should not be interrupted.
        }

        if(wasCancelled())
            Debug.WriteLine("Thread cancelled");
        else
            Debug.WriteLine("Thread completed");
    }
}
Run Code Online (Sandbox Code Playgroud)

这个例子确实使用了带有“外部闭包”的 lambda;如果我们的方法要在工作线程完成之前退出,则 lambda 将会出错,因为 cancel 变量已取消作用域并被销毁。在将此模型适应您的实际架构时,请记住这些事情;如果您在一种方法中启动线程并等待它在另一种方法中完成,同时在第三种方法中触发取消(实际上是一种相当常见的情况),则该标志必须位于工作线程之前不会被销毁的某个位置。结束执行。

  • 查看堆栈跟踪:如果在 SNINativeMethodWrapper.SNIReadSync 中调用 Thread.Abort() ,这在我看来是一个框架方法,因此您的答案没有帮助 (10认同)