【问题标题】:Timer to control the bluetooth scan time定时器控制蓝牙扫描时间
【发布时间】:2018-09-21 14:48:47
【问题描述】:

我目前正在使用蓝牙模块,我的应用程序会自动扫描该模块以自动连接。

我想要实现的是对允许应用程序扫描模块的时间量进行限制。我认为为此目的使用 system.threading.Timer 在代码后面运行是很有意义的。

if(c = 1)
    {
     bleText.Text = "Scanning...";
     Scan_Function(); 
    }
Private void Scan_Function()
{
 //Timer stuff
}

但是,我不知道这是否是正确的做法,以及我可能会如何做。

【问题讨论】:

    标签: c# xamarin xamarin.forms


    【解决方案1】:

    您不需要计时器,只需获取任务开始的时间并从任务结束的时间中减去它:

    Private void Scan_Function()
    {
        DateTime start = DateTime.Now;
        //Timer stuff
        double milliSecondsElapsed = (DateTime.Now - start).TotalMilliSeconds;
    }
    

    当然你也可以在方法之外使用这两行代码:

    DateTime start = DateTime.Now;
    Scan_Function()
    double milliSecondsElapsed = (DateTime.Now - start).TotalMilliSeconds;
    

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      相关资源
      最近更新 更多