如何查看某个值在一段时间内是否发生了变化?

时间:2019-02-05 14:41:03

标签: c#

我正在使用设置值的异步方法。我想看看该值在10秒内是否保持不变。如果该值在这段时间内没有变化,则应启用标志以执行其他操作。

有一种简单的方法吗? 计时器应该在更改值时启动并进行检查(例如,也许并非每次都过大,而是每0.5秒),直到计时器达到10秒,然后执行方法或将标志的值更改为false。

当我尝试使用另一种异步方法来更改值时无法触发它。在我看来,这很复杂。

    /// <summary>
    ///     Asynchronously sends the commands to CWI
    /// </summary>
    private async void SendCommandToCWI(object sender, EventArgs e)
    {
        await Task.Delay(1);
        System.Windows.Application.Current.Dispatcher.Invoke(() =>
        {

            //Here is some logic that will calculate many things to get the value of _axisX and _axisY.
           //I deleted this part to show the code as it was unnecessary. 

            if (!(_axisX == 0 && _axisY == 0) && (PortVM != null) && PortVM.IsOpen)
            {
                CommandSender.AxisX(_axisX);
                Thread.Sleep(50);
                string indata = PortVM.ReadExisting();
                //Debug.Print("Data Received:");
                //Debug.Print(indata);
                Console.WriteLine(_axisY);
                Console.WriteLine(_axisX);
                CommandSender.AxisY(_axisY);
            }
            else
            {
                PanelX = 0;
                PanelY = 0;
                _TimerInside.Stop();
                _axisX = 0;
                _axisY = 0;                       
                CommandSender.AxisX(0);
                CommandSender.AxisY(0);
                CommandSender.AxisX(_axisX);
                CommandSender.AxisY(_axisY);
            }
        });
    }

所以基本上我想看看是否每次我计算_axisX_axisY时,如果10秒钟后结果保持不变,则应该调用方法或将标志从true设置为false。 / p>

0 个答案:

没有答案