InvalidOperationException: The Undo operation encountered a context that is different from what was applied in the corresponding Set operation

半城伤御伤魂 提交于 2019-12-07 04:03:47

问题


I got the following exception:

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:  

The application is a Visual Studio 2005 (.Net 2.0) console application. It is a server for multiple TCP/IP connections, doing asynchronous socket reads and synchronous socket writes.

In searching for an answer I came across this post which talks about a call to Application.Doevents() which I don't use in my code.

I also found this post which has a resolution involved with Component which I also don't use in my code. The application does reference a library that I created that contains custom user controls and components, but they are not being used by the application.

Question: What caused this to happen and how do I prevent this from happening again? Or a more realistic question: What does this exception actually mean? How is "context" defined in this situation?

Anything that can help me understand what is going on would be very much appreciated.


回答1:


context refers to as "Thread Context" and is not in any way related to your code. However it is an internal issue with .net which could be only limited to beta editions



来源:https://stackoverflow.com/questions/2835273/invalidoperationexception-the-undo-operation-encountered-a-context-that-is-diff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!