程序有时会崩溃

时间:2013-11-27 07:38:46

标签: c# vb.net winforms error-handling crash

我有一个Windows窗体,目前从串口获取数据并获取数据。每隔一段时间将数据保存在xml中并保存到sql中。该程序有时会工作,而其他时候只会崩溃。这是错误的事件日志。

Application: appname
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.TimeoutException
Stack:
   at System.IO.Ports.SerialPort.ReadTo(System.String)
   at System.IO.Ports.SerialPort.ReadLine()
   at appname.frmParameters.mySerialPort_DataReceived(System.Object, System.IO.Ports.SerialDataReceivedEventArgs)
   at System.IO.Ports.SerialPort.CatchReceivedEvents(System.Object, System.IO.Ports.SerialDataReceivedEventArgs)
   at System.IO.Ports.SerialStream+EventLoopRunner.CallReceiveEvents(System.Object)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:2)

你在ReadTo(string)方法上得到了一个timeoutexception,所以你应该捕获并处理它。

try
{
    Readto(string);
}
catch(TimeoutException ex)
{
    //Handle the timeout exception
}