当主窗体最小化或移动的serialport从接收数据停止 [英] receiving data from serialport stops when main form is minimized or moved

查看:125
本文介绍了当主窗体最小化或移动的serialport从接收数据停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

爵士

我道歉,如果这已经涵盖某处。我做了搜索,找到一些东西,我已经实现了。

I apologize if this is already covered somewhere. I did a search and find something which I have already implemented.

我在开发的经由连接的serialport设备接收数据的应用程序。我使用的SerialPort DataReceived事件检索捕获数据。我要显示在主窗体的文本框的数据。数据被频繁收到。我用定时器将命令发送到设备,以应对设备发送一些数据。计时器间隔为100毫秒。在每100毫秒的时间间隔有些指令的发送和接收的数据相对应。我曾经调用函数来更新GUI元素文本框一样,标签等,一切都很好。所有的元素都是精美的更新。但是,接收数据,如果我做喜欢运动的形式,最小化,最大化,或单击窗体某处,然后数据接收站主任何形式上的变化过程中。我couldnot觉得为什么它发生的原因是什么?我也改变了计时器的时间间隔为200300400500,但同样的问题是存在的。

I am developing an application which receives data from a device connected via serialport. I am using SerialPort datareceived event to capture data. I am to display data in text boxes of the main form. Data is received frequently. I used Timer to send command to the device, in response device send some data. Timer interval is 100 ms. In every 100 ms interval some command is sent and correspondingly data is received. I used Invoke function to update the GUI elements like TExtbox, labels etc. Everything is fine. All the elements are updating beautifully. But during receiving data if I make any change in main form like moving form, minimizing, maximizing, or clicking somewhere in the form, then data receiving stops. I couldnot find the reason why its happening ? I also changed the timer interval to 200,300,400,500 but same problem is there.

请告诉我,为什么它的发生?和可能的解决方案...

Please tell me why its happening? And possible solution...

在此先感谢....:)

Thanks In advance.... :)

推荐答案

的问题是,最小化时计时器被禁用。你应该宁可创建一个新的,并在其中使用 Thread.sleep代码(100); 使其入睡。此外,中止它,当你关闭。考虑是这样的:

The problem is that Timer is disabled when minimized. You should rather create a new Thread and in it use Thread.Sleep(100); to make it sleep. Also, abort it when you are closing. Consider something like:

Thread recieverThread = new Thread(delegate()
  {
    try
    {
      //try loading data
      Thread.Sleep(100);
    }
    catch (ThreadAbortException)
    {
      //close port or something
    }
  });

//on form.close or something like that
recieverThread.Abort();

这应该做的伎俩。此外,如果你必须使用reciever更新接口 Form.Invoke(...)这样做,因为它是在单独的线程运行。

This should do the trick. Also if reciever updates interface you MUST use Form.Invoke(...) to do that because it is running on separate thread.

这篇关于当主窗体最小化或移动的serialport从接收数据停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆