The Undo operation encountered a context that is different from what was applied in the corresponding Set operation.  
Author Message
Jordy Boom





PostPosted: .NET Framework Networking and Communication, The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. Top

Hello,

I have tried to find a solution for this problem but so far have not found anything that applies to my exception.

This is the message in my 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)."

It occurs right after I perform any calls on a Socket that use threading (Begin...). The exception occured suddenly after I upgraded from .NET 2.0 beta2 to the final version of .NET 2.0, consider the following code.

System.Net.EndPoint ep;
ep = new System.Net.IPEndPoint(host, Convert.ToInt32(port));
mainSock = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
mainSock.BeginConnect(ep, new System.AsyncCallback(DoneConnecting), mainSock);

(host and port are Strings, mainSock is a socket) During execution, right after the execution point leaves the function (there is no code following this in the function) the De**** comes up with the following error:

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)."
Source="mscorlib"
StackTrace:
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.Dns.ResolveCallback(Object context)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)

People have commented on the DNS line, however, this code is not executed until the DNS has finished resolving the address. However, should it apply, here is the DNS lookup code I use.

Dns.BeginGetHostEntry(Host, new AsyncCallback(DoneWithDNS), mainSock);

Then after it finishes, this is how I process the result:

ih = Dns.EndGetHostEntry(ar);
ia = ih.AddressList;
host = (IPAddress)ia.GetValue(0);

The problem is that the spot the Exception is thrown is in [External Code], so I don't know anything about the area it is going wrong. If I convert my connect to not use BeginConnect(), the problem pops up later when I use BeginReceive(). I have noticed that sometimes, if I put a breakpoint in the function with BeginConnect(), and I take a really long time to step through the code, it will continue without a problem. It does however raise the same Exception once it tries to execute BeginReceive().

I'm at a total loss as to what I should or can do. My guess is that it's possible that there is a problem with Threads and Sockets, but I really don't know for sure.

My project is at a total stand still as it is an important component. Reverting back to using non Begin..() functions basically "fixes" the problem, but it does not help me at all. I need this functions to execute and have my program respond as well.

.NET Framework 2.0 and Visual C# 2005 Express



.NET Development33  
 
 
Mike Flasko





PostPosted: .NET Framework Networking and Communication, The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. Top

Are you still having this issue Do you have a snippet of code that we can run to repro this issue

 
 
Jordy Boom





PostPosted: .NET Framework Networking and Communication, The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. Top

I solved the problem by checking in each method whether I needed to call Invoke using this.InvokeRequired.

It's not a very elegant solution but it worked.

private delegate void IAsyncResultDelegate(System.IAsyncResult x);

if (this.InvokeRequired) {
// BeginInvok
this.Invoke(new IAsyncResultDelegate(DoneConnecting), ar);
return;
}


I could try later to provide a code sample although I know I've tried reproducing it and it worked fine then. It might just be a specific problem that I don't know about. But I know other people have had this problem and the solution is hard to find/not clear. Perhaps it would be a good idea to look at the code that causes this exception and perhaps clarify what is going wrong. Or an article explaining what it means exactly...

Thanks.