我有一个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()
非常感谢任何帮助,谢谢
最佳答案
您在 ReadTo(string) 方法上遇到超时异常,因此您应该捕获并处理该异常。
try
{
Readto(string);
}
catch(TimeoutException ex)
{
//Handle the timeout exception
}
关于c# - 程序有时会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20236482/