【问题标题】:Program will sometimes crash [closed]程序有时会崩溃[关闭]
【发布时间】:2013-12-12 17:47:46
【问题描述】:

我有一个 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()

感谢您的帮助

【问题讨论】:

  • 我们需要程序崩溃的源代码。如果您还将错误消息放入描述中将不胜感激(例如,如果您的异常被称为 e,您必须添加 e.Message、e.Source 和 e.StackTrace
  • 在我目前的情况下,我无法确定错误位置,因为此事件失败错误来自已编译的 .exe。是否有一个整体 wrap 命令可用于将错误输出到日志文件中?我无法从源代码运行它,需要编译它来测试它。

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


【解决方案1】:

您在 ReadTo(string) 方法上遇到超时异常,因此您应该捕获并处理它。

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

【讨论】:

  • 现在将执行此操作,并在我遇到异常时更新。现在无法复制错误,
  • 它应该很容易复制 - 当串行端口上没有可用数据时抛出 TimeoutException。所以停止 COM 端口的另一端发送数据,你应该得到你的 TimeoutException。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多