延迟执行UI过程。 [英] Delay in performing UI process.

查看:95
本文介绍了延迟执行UI过程。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我用C#开发了一个应用程序。该应用程序的开发是为了满足MODBUS通信的需要。基本上我正在向奴隶发送查询。使用1个计时器获得响应&根据收到的数据,我试图在UI中刷新按钮。我的问题是按钮闪烁的过程太慢了。所以我想知道它的可能原因。以下代码显示了投票过程&闪烁事件也是。



Hi,
I have developed an application using C#. The application is developed to serve the need for MODBUS communication. Basically i am sending queries to the slaves & getting response using 1 timer & based on the data received i am trying to flash the button in the UI. My problem the process of flashing of button is too slow. So i wanted to know the probable reason for it. Following code shows the polling process & flashing event also.

private void StartPoll()
{
   pollCount = 0;
   slaves = 0;


   if (mb.Open(cboPort.SelectedItem.ToString(), Convert.ToInt32(cboBaud.SelectedItem.ToString()),
       8, Parity.None, StopBits.One))
   //Now open The Port
   {
       //Disable double starts:
       btnstart.Enabled = false;
       btnstop.Enabled = true;

       //Set polling flag:
       isPolling = true;

       //Start timer using provided values:
       timer.AutoReset = true;
       timer.Interval = 500;
       timer.Start();            
    }

}

 #region Timer Elapsed Event Handler
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    PollFunction();
}
#end region

# region Pollingfuntion
private void PollFunction()
{
   //Update GUI:
   DoGUIClear();
   pollCount++;
 

   //Create array to accept read values:
   string[] rs485data = new string[72];

   ushort pollStart;
   ushort pollLength;

   pollStart = 0;
   pollLength = 72;

   string stringTocheck0 = "0";
   string stringTocheck1 = "1";
   string stringTocheck2 = "2";
 

   SID = Convert.ToByte(slaveaddress[slaves]);
   add = slaves;


   //Read registers and display data in desired format:
   try
   {
       while (!mb.SendFc3(SID, pollStart, pollLength, ref values)) ;
   }

   catch (Exception err)
   {
       DoGUIStatus("Error in modbus read: " + err.Message);
   }

   slaves++;

   for (int s = 0; s < 72; s++)
   {
       rs485data[s] = Convert.ToString(values[s + 1]);
   }

   switch (dataType)
   {

       case "Decimal":

           if (rs485data.All(s =>; s.Contains(stringTocheck0)) == true)
           {
               errorIndexZero[add] = SID;
               this.Invoke(new EventHandler(ResetChangeBtnColor));
           }

           else if (rs485data.Any(s =>; s.Contains(stringTocheck1)) == true)
           {
               errorIndexOne[add] = SID;
               this.Invoke(new EventHandler(TestChangeBtnColor));                   

           }
           else if (rs485data.Any(s =>; s.Contains(stringTocheck2)) == true)
           {
               errorIndexTwo[add] = SID;
               this.Invoke(new EventHandler(AcceptChangeBtnColor));
           }

           break;
   }


   if (slaves == devicescount1)
   {
       slaves = 0;       
   }
}
#end region

public void TestChangeBtnColor(object sender, System.EventArgs e)
{
   timer2.Enabled = true;           
}
private void timer2_Tick(object sender, EventArgs e)
{
   for (int n = 0; n < btn.Length; n++)
   {
        
         if (errorIndexOne[n]!=0)
          {
              if (btn[n].BackColor == Color.LightGreen)
               {
                   btn[n].BackColor = Color.Red;
               }
               else
               {
                   btn[n].BackColor = Color.LightGreen;
               }
          }

      }
}

推荐答案

全部删除计时器和使用线程来完成任务。线程提高了执行的效率和速度。
Remove all the timers & use threads to achieve the task. Threads increase the efficiency & speed of execution.


这篇关于延迟执行UI过程。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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