我正在为10个tcp客户端使用自定义客户端连接。
bool _flagScanner1Connect = true;
private xyzTranmissionProtocol xyz1TranmissionProtocol = null;
private static readonly object LockWhileConnectingForScanner1 = new object();
internal ICustomClient Scanner1Conn;
System.Threading.Timer _tmrCheck1ClientConn;
public void Start1ScannerConn()
{
try
{
_tmrCheck1ClientConn = new System.Threading.Timer(Check1ScannerConn, null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(5));
}
catch (Exception )
{
}
}
private void Check1ScannerConn(object state)
{
if (!_flagScanner1Connect)
return;
lock (LockWhileConnectingForScanner1)
{
if (Scanner1Conn == null || Scanner1Conn.CommunicationState == ClientCommunicationStates.Disconnected)
{
try
{
_tmrCheck1ClientConn.Change(Timeout.Infinite, Timeout.Infinite);
Scanner1Conn = CustomClientFactory.CreateClient(new CustomClientTcpEndPoint(“Scanner IpAdress”, “Scanner Port Number”));
Scanner1Conn.Connected += Scanner1Conn_Connected; ;
Scanner1Conn.Disconnected += Scanner1Conn_Disconnected;
Scanner1Conn.MessageReceived += Scanner1Conn_MessageReceived;
xyz1TranmissionProtocol = new xyzTranmissionProtocol();
Scanner1Conn.TransmissionProtocol = xyz1TranmissionProtocol;
.Connect();
//SEND A PING MESSAGE
_tmrCheck1ClientConn.Change(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
}
catch (Exception )
{
if (_flagScanner1Connect)
try
{
Scanner1Conn?.Disconnect();
}
catch { }
try
{
Scanner1Conn?.Dispose();
}
catch { }
try
{
Scanner1Conn = null;
}
catch { }
}
finally
{
_tmrCheckClientConn.Change(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
}
}
}
}
我为10个条形码扫描器编写了10种不同的功能。如果我使用3或少于3,我的应用程序将正常工作。但是,如果我使用3个以上的扫描仪,则不会触发messagerecived事件。并且应用程序将变得非常慢。 谁能帮我解决这个问题。