[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1440
  • Last Modified:

Undo Operation Encountered Exception

I have created an application using asynchronous sockets that starts a local server, allows clients to connect to it. When the server starts, it creates an inherited version of my client class to create a connection to a unix server. There is no real difference in the connection, only the way data is handled when it comes back. When the server starts, the unix client creates a connection through to the remote point, sends a message and receives a response. This works fine. When my windows based client connects to the server, it sends a message on connect as well, the server receives this and then forwards a message to the unix client to tell it to forward a wakeup to the unix system. On receipt of the message back from unix acknowledging this, the server crashes with the following message.

System.InvalidOperationException was unhandled
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)."
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)

There is no indication of whereabouts in the program this happens, and the only other references to this problem I can find (forums.microsoft.com/msdn) have either no solution, or the solution makes no sense within my application.

In my send method, I set a waiting state, and within that loop I have an event in which I have Application.DoEvents( ) (I found it hangs otherwise and won't output anything until things have finished) removing that doesn't fix things. And any interaction I am having with the GUI at all I have wrapped in invoke (i only output log data to a richtext box). I'm at a loss as to how to proceed with this problem, and I don't know what I can change to fix it!

Any help appreciated.
1 Solution
Removing all occurences of Application.DoEvents() seems to solve this specific problem.


  Nayer Naguib

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now