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