BeetleX
BeetleX copied to clipboard
一个封锁操作被对 WSACancelBlockingCall 的调用中断
我用TCP Client 出现如下错误 一个封锁操作被对 WSACancelBlockingCall 的调用中断
这个是.net 内部引发的错误,请问下你是怎样使用beetlex tcp client
public class TCPCamera { private TcpClient client;
private bool TaskIsRunning = true;
static TCPCamera instance;
public Action<string> ReceiveTCP;
public Action<bool> TCPConnectStatus;
public bool IsConnect = false;
public static TCPCamera GetInstance()
{
if (instance == null)
{
instance = new TCPCamera();
}
return instance;
}
public void Connect()
{
Task.Run( () =>
{
RunClient();
});
}
private void RunClient()
{
string ClassName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
string FuncName = System.Reflection.MethodBase.GetCurrentMethod().Name;
string msg = "";
string strmsg1 = $"0 { util.GetNow()} connectCamera ";
App.Log.Info(strmsg1);
string IP = Config.GetInstance().getConfig("CameraIP");
int Port = Convert.ToInt32(Config.GetInstance().getConfig("CameraPort"));
if (client == null)
{
client = SocketFactory.CreateClient<TcpClient>(IP, Port);
client.TimeOut = 300000;
client.Encoding = Encoding.GetEncoding(936);
}
while (TaskIsRunning)
{
try
{
var read = client.Receive();
var str = read.ReadToEnd();
msg = @$"
Class Name [{ClassName}]
Function [{FuncName}]
RecvTime [{util.GetNow()}]
Parameter
Message 接收Vision命令 [{str}]";
App.Log.Info(msg);
ReceiveTCP(str);
TCPConnectStatus(client.IsConnected);
}
catch (Exception e)
{
TCPConnectStatus(false);
msg = @$"
Class Name [{ClassName}]
Function [{FuncName}]
RecvTime [{util.GetNow()}]
Parameter
Message RunClient 视觉网络异常 : {e.Message} HResult: {e.HResult}
";
App.Log.Error(msg);
}
}
}
public void Send(string cmd)
{
try
{
TCPConnectStatus(client.IsConnected);
if (client.IsConnected)
{
client.Stream.ToPipeStream().Write(cmd+"\r\n");
client.Stream.Flush();
string ClassName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
string FuncName = System.Reflection.MethodBase.GetCurrentMethod().Name;
string msg = @$"
Class Name [{ClassName}]
Function [{FuncName}]
RecvTime [{util.GetNow()}]
Parameter
Message 发送命令到视觉 [{cmd}]";
App.Log.Info(msg);
}
else
{
string ClassName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
string FuncName = System.Reflection.MethodBase.GetCurrentMethod().Name;
string msg = @$"
Class Name [{ClassName}]
Function [{FuncName}]
RecvTime [{util.GetNow()}]
Parameter
Message 视觉网络未连接,命令发送失败 [{cmd}]
";
App.Log.Warn(msg);
}
}
catch (Exception ex)
{
string ClassName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
string FuncName = System.Reflection.MethodBase.GetCurrentMethod().Name;
string msg = @$"
Class Name [{ClassName}]
Function [{FuncName}]
RecvTime [{util.GetNow()}]
Parameter
Message 视觉通讯出现异常 [{ex.Message}]";
App.Log.Error(msg);
}
}
public void Dispose()
{
TaskIsRunning = false;
client.DisConnect();
client.Dispose();
client = null;
}
}
pipestream不是线程安全的,Send方法不能多线程发数据。 看一下具体的socketException errorcode是多什么
[ 一个封锁操作被对 WSACancelBlockingCall 的调用中断。 HResult: -2147467259]
看能不能把异常转换成socketexception看对应的errorcode
找到一个winsock描述,应该是说不要在gui线程中使用对应的同步阻塞 The WSACancelBlockingCall function has been removed in compliance with the Windows Sockets 2 specification, revision 2.2.0.
The function is not exported directly by WS2_32.DLL and Windows Sockets 2 applications should not use this function. Windows Sockets 1.1 applications that call this function are still supported through the WINSOCK.DLL and WSOCK32.DLL.
Blocking hooks are generally used to keep a single-threaded GUI application responsive during calls to blocking functions. Instead of using blocking hooks, an applications should use a separate thread (separate from the main GUI thread) for network activity.