我在线程中用事件响应的方式处理数据,共有10个事件。其中有6个是一秒发一次事件消息,有四个是一秒十次发事件消息。结果发现线程来不及处理有些消息,造成一些数据丢失,请问有什么办法解决?

解决方案 »

  1.   

    我现在把四个一秒十次的数据处理放在OnIdle()中,这样就造成不能实时显示。另外我在OnIdle()函数中还有不少任务,包括对二十多个文件的写操作,请问这地程序有没有影响,我看网上说不宜在OnIdle()中处理太多任务。我程序主要实现数据的监听录取。请高手帮忙!
      

  2.   

    我也尝试过队列,我用动态数组来实现。将串口收到的数据放到动态数组里,然后在对应的事件处理中我用如下代码实现:
    int arraysize =  CarrayBattle2Gun1.GetSize();
    for(int i=0;i<arraysize;i++)
    {
      memcpy(Rbuff1,CarrayBattle2Gun1[0].Info,nLength);//将动态数组中数据转移到数组中
      Battle2Gun1();//对数据进行处理
      CarrayBattle2Gun1.RemoveAt(0);//删除动态数组中的数据
    }但问题又来了,有可能队列数据太多,占用过多时间,影响其他事件的处理。
      

  3.   

    If there is really more data than you can process, than there is nothing you can do but to drop some of them.
    If the data drop are because of short burst high data rate, you could try to same them first and process later. But if data are comming at a steady rate and there is not enough CPU resource to handle them, this could consume all your memory. Or, you can use a ring buffer to store the data. When the buffer is full, just drop the newly arrived data or overwrite the old data.
    If you have more than one CPU, you can use more than one data pool and more than one thread to process them.