我在这里的第一篇文章,但这个网站已经回答了我以前有过的许多问题。希望我可以提供足够的细节来解释我所面临的问题,因为我不完全理解.NET如何处理我创建的线程!System.Threading.Timer导致其他计时器落后
好吧,所以基本上,我有一个线程设置为每1000ms运行一次从视频编码器获取帧计数器并计算FPS。尽管我意识到它不准确(通常在事件之间超过1000毫秒),但对于现在的System.Threading.Timer来说,精度足够了。我还有另一个Threading.Timer正在运行并从网络读取串行设备。问题是,如果网络设备变得不可用,并且该定时器上的套接字超时,则FPS定时器将完全不同步!所以他们以前每1015ms执行一次(测量),但是当我启动另一个Thread.Timer尝试进行套接字连接并且失败时,它会导致FPS计数器定时器完全关闭(最高7000ms!)。我不太清楚为什么会这样,而且真的需要FPS计数器几乎无论如何运行一次。的代码
位 - >
FPS计数器
private void getFPS(Object stateInfo)//Run once per second
{
int frames = AxisMediaControl.getFrames; //Axis Encoder media control
int fps = frames - prevValue;
prevValue = frames;
setFPSBar(fps, fps_color); //Delegate to update progress bar for FPS
}
电池电量定时器
while (isRunning)
{
if (!comm.Connected) //comm is standard socket client
comm.Connect(this.ip_address, this.port); //Timeout here causes other timer threads to go out of sync
if (comm.Connected)
{
decimal reading = comm.getBatt_Level();
//Calculate Readings and update GUI
Console.Out.WriteLine("Reading = " + (int)prog);
break;//Debug
}
这是用来连接到插座当前的代码 - >
public Socket mSocket { get; set; }
public bool Connect(IPAddress ip_address, UInt16 port)
{
try
{
mSocket.Connect(ip_address, port);
}
catch(Exception ex)
{
}
return mSocket.Connected;
}
Hope完全不太含糊!
为什么隐藏异常是一个好主意? – 2012-04-08 21:37:00
该软件将用于会出现很多超时的环境中,并且我认为每5秒钟不会显示一条消息会很有用。我在调试时发现异常,但我期望连接失败,因为网络是无线网络,并且在操作期间预计会出现退出。你会建议什么? – jackocurly0074 2012-04-08 21:40:08
我建议捕捉'TimeoutException'或任何你关心的特定异常,并允许其他人传播。这样,你会了解其他例外情况。 – 2012-04-08 23:56:09