要求一直同步而且稳定读取外部设备的频率..另外有个form记录这个频率..我的朋友说只用do while...可以准确读取,但是form会没响应. 后来我用了thread 加do while可以读取...但是当高频的时候会慢....还有就是把程序最小化后...又会比实际的快....这个问题怎么解决????有谁做过类似的程序??可以请教下吗???只用do while读取的程序
    public partial class Form1 : Form
    {
        float count = 0;        
        public Form1()        {
         
            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)
        {           
            PortAccess.Output(888,255);
                           }
        private void button1_Click(object sender, EventArgs e)
        {
            PortAccess.Output(888, 0);            //textBox2.Text = Convert.ToString (PortAccess.Output(888,0);       
                    }        private void button3_Click(object sender, EventArgs e)
        {
            int y = 126;
           do
           {
                 y = PortAccess.Input(889);
                textBox1.Text = Convert.ToString(y);
             
                textBox1.Refresh();                
           } while (y == 120 || y == 56);
            }
    }
 }
thread + do while    public partial class DistancePulse : UserControl
    {
        double i = 0;
        string direction = "1";
      public  double stopechain = 0;
        int y;
        Thread pulsesThread;
      
        public DistancePulse()
        {
            InitializeComponent();
       
        }        Boolean  count120 = false;        private void countPulses()
        {
           
            do
            {
              
              y = PortAccess.Input(889);              switch (y)
              {                  case 120: count120 = true; break;
                           }              if (y == 56 && count120 == true )
              {
                    switch (direction)
                      {                  case "1": count120 = false; i++; break;
                  case "2": count120 = false; i--; break;                       }
                  
              }                 } while ((y == 80) || (y == 16) || (y == 48) || (y == 24) || (y == 112) || (y == 88) || (y == 120) || (y == 56));
        }// end method  
        private void timer1_Tick(object sender, EventArgs e)
        {
   
            Refresh();          
        }
        protected override void OnPaint(PaintEventArgs e)
        {
        
            if (i >= 0)
            {
              
                string s = i + "";
                s = s.PadLeft(7, '0');
                string s1 = s.Substring(1, 3);
                string s2 = s.Substring(4);
                string s3 = s1 + "." + s2;                lbl_display.Text = s3;
        
            }
            else {
              double  itemp = Math.Abs(i);              string s = itemp + "";
                s = s.PadLeft(7, '0');
                string s1 = s.Substring(1, 3);
                string s2 = s.Substring(4);
                string s3 = "-"+s1 + "." + s2;                lbl_display.Text = s3;
            
            
            }        }        private void btn_play_Click(object sender, EventArgs e)
        {
          pulsesThread = new Thread(new ThreadStart(countPulses));
          pulsesThread.Start();
       
          timer1.Enabled = true;
        }
        private void DistancePulse_Load(object sender, EventArgs e)
        {
            PortAccess.Output(888, 0);
          
        }
  
    }//End Class
}thread + do whiel 测试结果2Hz ------>   20
3Hz            30
4Hz            40
5Hz            50
6Hz            56
7Hz            57
8Hz            63
9Hz            55
10Hz          7012Hz         75
15Hz         86
20Hz         122from here it seems that the program can catch up if the frequency is below 5Hzhowever i notice this:
it will run faster if we leave the program on the backgroundfor example: 
The video counter is 6.459 and the current counter reading is 6.390
(obviously the counter is running too slow)and i browse the internet for say... 2 minutes (and keep the counter running on the background), then the video counter has reached 6.900, but your counter is counting 7.030, which is becoming too fast. If I leave the counter on the background for longer time (10 minutes) then the differences between the real video counter and your counter getting bigger (e.g. video counter 7.900 and your counter 8.600)

解决方案 »

  1.   

    看了这个标题,认为很难,让我不敢进来假如正常访问并口能达到n次/秒。启动一个线程作为定时器,然后以1/n秒时间长度定时发送自定义消息,另外有程序订阅这个消息,当定时消息到达的时候启动读取并口程序进行并口读取。请搂主去搜索一下“完成端口”,使用循环之所以会慢,原因是因为循环程序会占用很大的机器资源,如果使用完成端口概念就会大大提升效率。Windows的消息机制就可以实现这样的效果。试想,我们的电脑只要是开起来了,并且使用Windows,消息总是满天飞的,可是Windows也总是处理得过来。使用自定义消息机制我有用过,我的程序需要完成两个与服务器之间的TCP连接,其中一个发送指令到服务器,另外一个接收服务器返回的数据,在使用这种模式之前,即使不往服务器发送消息,也没有服务器返回的消息接收,使用线程也会占用我的2/3机器资源,并且经常发生服务器返回指令接收不及时现象,后来我改了消息机制,效果很好。