OK. 下面是代码: public void Poll() { foreach (var station in stations) { ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPollCallback), station); } if (WaitHandle.WaitAll(doneEvent.Values.ToArray(), millsecondsTimeout, false)) { int availabeThreads,aiot; ThreadPool.GetAvailableThreads(out availabeThreads, out aiot); Console.WriteLine("poll end" + index.ToString()); } else { Console.WriteLine("timeout"); } } public void ThreadPollCallback(object state) { var station = state as Station; try { ushort[] res = ReadWaveData(station.IP); lock (lockObj) { threads[station.Id] = Thread.CurrentThread; Data[station.Id] = new List<ushort>(res); stationErrNum[station.Id] = 0; Exceptions[station.Id] = null; } } catch (Exception e) { Console.WriteLine("comm thread error"); lock (lockObj) { threads[station.Id] = Thread.CurrentThread; Exceptions[station.Id] = e; stationErrNum[station.Id]++; if (stationErrNum[station.Id] > 3) { Data[station.Id] = new List<ushort>(12); ; } } } finally { doneEvent[station.Id].Set(); } } public ushort[] ReadWaveData(string ipAddress) { ushort[] inputs; using (TcpClient client = TcpClientConnector.Connect(ipAddress, 3100, 500)) { var ns = client.GetStream(); var bw = new System.IO.BinaryWriter(ns); bw.Write(COMMAND_READWAVEDATA, 0, 6); bw.Flush(); } return inputs; }
OK. 下面是代码: public void Poll() { foreach (var station in stations) { ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPollCallback), station); .........你忘记贴你说的“抛出异常的那一行代码”了。
OK. 下面是代码: public void Poll() { foreach (var station in stations) { ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPollCallback), station); .........你忘记贴你说的“抛出异常的那一行代码”了。 就是using (TcpClient client = TcpClientConnector.Connect(ipAddress, 3100, 500))这里。
using(var client=new tcpclient(ipaddress,port))
{
client.getstream().write(data);
}
using(var client=new tcpclient(ipaddress,port))
{
client.getstream().write(data);
}
下面是代码:
public void Poll()
{
foreach (var station in stations)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPollCallback), station);
}
if (WaitHandle.WaitAll(doneEvent.Values.ToArray(), millsecondsTimeout, false))
{
int availabeThreads,aiot;
ThreadPool.GetAvailableThreads(out availabeThreads, out aiot);
Console.WriteLine("poll end" + index.ToString());
}
else
{
Console.WriteLine("timeout"); }
}
public void ThreadPollCallback(object state)
{
var station = state as Station;
try
{
ushort[] res = ReadWaveData(station.IP);
lock (lockObj)
{
threads[station.Id] = Thread.CurrentThread;
Data[station.Id] = new List<ushort>(res);
stationErrNum[station.Id] = 0;
Exceptions[station.Id] = null;
}
}
catch (Exception e)
{
Console.WriteLine("comm thread error");
lock (lockObj)
{
threads[station.Id] = Thread.CurrentThread;
Exceptions[station.Id] = e;
stationErrNum[station.Id]++;
if (stationErrNum[station.Id] > 3)
{
Data[station.Id] = new List<ushort>(12); ;
}
}
}
finally
{
doneEvent[station.Id].Set();
}
}
public ushort[] ReadWaveData(string ipAddress)
{
ushort[] inputs;
using (TcpClient client = TcpClientConnector.Connect(ipAddress, 3100, 500))
{
var ns = client.GetStream();
var bw = new System.IO.BinaryWriter(ns);
bw.Write(COMMAND_READWAVEDATA, 0, 6);
bw.Flush();
}
return inputs;
}
下面是代码:
public void Poll()
{
foreach (var station in stations)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPollCallback), station);
.........你忘记贴你说的“抛出异常的那一行代码”了。
下面是代码:
public void Poll()
{
foreach (var station in stations)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPollCallback), station);
.........你忘记贴你说的“抛出异常的那一行代码”了。
就是using (TcpClient client = TcpClientConnector.Connect(ipAddress, 3100, 500))这里。