c#中socket建立连接之后,出现InvalidOperationException异常;详细如下:未处理 System.InvalidOperationException
Message="Undo 操作遇到的上下文与相应 Set 操作中应用的上下文不同。可是因为在线程上 Set 了一个上下文但未进行还原(撤消)。"
Source="mscorlib"
StackTrace:
在 System.Threading.SynchronizationContextSwitcher.Undo()
在 System.Threading.ExecutionContextSwitcher.Undo()
在 System.Threading.ExecutionContext.runFinallyCode(Object userData, Boolean exceptionThrown)
在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteBackoutCodeHelper(Object backoutCode, Object userData, Boolean exceptionThrown)
在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Net.ContextAwareResult.Complete(IntPtr userToken)
在 System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
在 System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
在 System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)如果接着往下运行的话,会出现像提示IE出错一样的提示.
Message="Undo 操作遇到的上下文与相应 Set 操作中应用的上下文不同。可是因为在线程上 Set 了一个上下文但未进行还原(撤消)。"
Source="mscorlib"
StackTrace:
在 System.Threading.SynchronizationContextSwitcher.Undo()
在 System.Threading.ExecutionContextSwitcher.Undo()
在 System.Threading.ExecutionContext.runFinallyCode(Object userData, Boolean exceptionThrown)
在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteBackoutCodeHelper(Object backoutCode, Object userData, Boolean exceptionThrown)
在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Net.ContextAwareResult.Complete(IntPtr userToken)
在 System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
在 System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
在 System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)如果接着往下运行的话,会出现像提示IE出错一样的提示.
我的程序里有三次建立连接,只有这一次会出现这种问题.
连接函数如下:
public void ConnectTelnet()
{
stbTestProcess.Text = "正在连接TELNET";
string host_ip = gVariable.str_ip;
short port = 23;
//IPHostEntry IPHost = Dns.Resolve(host_ip); //获取主机
string[] aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;
IPEndPoint ep = new IPEndPoint(addr[0], port);
//新建一个Socket对象
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Blocking = false;
callbackProc = new AsyncCallback(ConnectCallback);
sock.BeginConnect(ep, callbackProc, sock); //异步传输,连接方法
} 多谢!
我的QQ是:273088597;
2.如果我只两次连接也是同样的问题,也就是说只有这次连接之后会出现这个问题。
3.软件是在.net2003上实现的,以前从来没出现过这种问题;只是最近在有一台机子上运行时提示:xxx.exe遇到问题需要关闭,错误是否要发送(就是跟我们IE遇到问题是提示的一样)。由于是只有一台机子会出现这个问题,而且在我本人机子上也没出现这个问题,因此我就用.net2005来调试试一下,就出现上面描述的问题。所以我肯定不是那台计算机的问题;是我的代码问题,而且就在socket连接之后。
DoEvents();引起的,去掉这句就正常了.