asp.net – “线程被中止了什么”. ‘SNIReadSync(SNI_Conn *,SNI_Packet **,
发布时间:2021-01-12 10:48:33 所属栏目:asp.Net 来源:互联网
导读:你好, 在ASP.Net WebApp(使用SQL Server 2008)中,当处理大量数据时,我会收到以下异常,并且在代码中的随机位置看来这个异常被抛出. 这个异常是什么意思?是超时吗? Thread was being aborted. at SNIReadSync(SNI_Conn* , SNI_Packet** , Int32 ) at SNINativ
你好, 在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,Boolean returnStream,Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,String method,DbAsyncResult result) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String method) at System.Data.SqlClient.SqlCommand.Exec 谢谢! 解决方法“线程正在中止”99%的时间是由您的代码使用Thread.Abort()引起的,在任何其他情况下终止进程,而不是灾难性故障. Thread.Abort是邪恶的,因为它从它自己的执行代码之外的线程中注入一个异常,因此期望和优雅地处理它是非常困难的,甚至是不可能的.如果您在另一个线程中运行此代码(良好的选择BTW; DB操作是多线程的天然候选),请勿使用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"); } } 这个例子确实使用了一个带有“external closure”的lambda;如果我们的方法在工作线程完成之前退出,则lambda将会出错,因为取消变量已被取消并被破坏.在将这种模型适应您的实际架构时,请牢记这些事项;如果你在一个方法中启动线程并等待它在另一个方法中完成,同时在第三个(实际上是相当常见的情况)触发取消的情况下,该标志必须存在于不会在工作线程有结束执行. (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 你如何指定在列表框中显示多少项目(高度)
- asp.net-mvc-3 – 如何在页面提交ASP.Net MVC时捕获哪个单选
- 所有我使用IO的动作都是异步的?
- asp.net – 如何正确地大写希腊字在.NET?
- asp.net-mvc – 使用IIS基本身份验证的OWIN身份验证
- 认证 – WebApi ActionFilterAttribute,HttpActionContext访
- 对asp.net网站进行基准测试,我可以使用jmeter吗?
- asp.net-mvc-2 – 使用’class(或其他保留关键字)作为匿名类
- ASP.NET SQL成员资格表
- asp.net-mvc-3 – 方法“OrderBy”必须在方法“跳过”异常之
推荐文章
站长推荐
- asp.net-mvc – Mvc 3 Razor:使用部分部分视图?
- asp.net+ajaxfileupload.js 实现文件异步上传代码
- asp.net-web-api – 在ASP.NET Web API控制器的n
- 如何保护我的ASP.NET AJAX应用程序?
- ASP.NET Core中调整HTTP请求大小的几种方法详解
- asp.net-mvc – DDD原理和ASP.NET MVC项目设计
- 使用ASP.NET AJAX Control Toolkit设置焦点
- asp.net – App Settings和connectionStrings配置
- asp.net-mvc-3 – 使用@ Html.Raw有风险吗?
- 如何解决.Net中冲突的程序集?
热点阅读