Undo Operation Encountered Exception

Posted on 2006-05-17
Last Modified: 2007-12-19
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 ( 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.
Question by:StealthBanana
    1 Comment
    LVL 14

    Accepted Solution

    Removing all occurences of Application.DoEvents() seems to solve this specific problem.


      Nayer Naguib

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    withoutString  challenge 40 121
    map interface methods 3 46
    ClickOnce Install - Shortcut Question 3 43
    changePi Challenge 15 57
    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now