gpt4 book ai didi

asp.net - 'Thread was being aborted.' 'at SNIReadSync(SNI_Conn* , SNI_Packet** , Int32 )' 是什么意思?

转载 作者:行者123 更新时间:2023-12-02 08:50:33 25 4
gpt4 key购买 nike

当处理大量数据时,我在 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

最佳答案

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

如果您在另一个线程中运行此代码(顺便说一句,不错的选择;数据库操作是多线程的自然候选者),请勿使用 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");
}
}

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

关于asp.net - 'Thread was being aborted.' 'at SNIReadSync(SNI_Conn* , SNI_Packet** , Int32 )' 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4389582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com