InvalidOperationException:撤消操作遇到的上下文与相应的Set操作中应用的上下文不同

时间:2010-05-14 15:17:23

标签: c# sockets asynchronous console-application

我遇到以下异常:

Exception Type: System.InvalidOperationException  
Exception Message: The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was Set on the thread and not reverted(undone).  
Exception Stack:   at System.Threading.SynchronizationContextSwitcher.Undo()  
at System.Threading.ExecutionContextSwitcher.Undo()  
at System.Threading.ExecutionContext.runFinallyCode(Object userData, Boolean exceptionThrown)  
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteBackoutCodeHelper(Object backoutCode, Object userData, Boolean exceptionThrown)  
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)  
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)  
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)  
at System.Net.ContextAwareResult.Complete(IntPtr userToken)  
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)  
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)  
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)  
Exception Source: mscorlib  
Exception TargetSite.Name: Undo  
Exception HelpLink:  

该应用程序是Visual Studio 2005(.Net 2.0)控制台应用程序。它是用于多个TCP / IP连接的服务器,执行异步套接字读取和同步套接字写入。

在搜索答案时,我遇到了this post,其中谈到了对Application.Doevents()的调用,我在代码中没有使用该调用。

我还发现this post有一个涉及Component的解决方案,我也没有在我的代码中使用它。该应用程序确实引用了我创建的包含自定义用户控件和组件的库,但它们未被应用程序使用。

问题:导致这种情况发生的原因是什么?如何防止这种情况再次发生? 或者一个更现实的问题:这个例外究竟意味着什么?在这种情况下如何定义“上下文”?

任何可以帮助我理解正在发生的事情都会非常感激。

1 个答案:

答案 0 :(得分:0)

context指的是“线程上下文”,与您的代码无任何关联。 然而,这是.net的内部问题,可能仅限于测试版