简体   繁体   English

计时器来控制蓝牙扫描时间

[英]Timer to control the bluetooth scan time

I'm currently working with a Bluetooth module, for which my application automatically scans in order auto-connect. 我目前正在使用蓝牙模块,我的应用程序将对其进行自动扫描以自动连接。

What i'm trying to achieve though, is to implement a restriction in terms of the amount of time that the application is allowed to scan for the module. 我要实现的目标是对应用程序被允许扫描模块的时间进行限制。 I figured that it made good sense to use system.threading.Timer for this purpose, to run behind code. 我认为,为此目的使用system.threading.Timer来运行代码是很有意义的。

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

However, i'm unaware if this is the correct way of doing it, and how i might be done. 但是,我不知道这是否是正确的方法以及如何完成。

You don't need a timer, just get the time when task starts and subtract it from the time it ends: 您不需要计时器,只需获取任务开始的时间并从任务结束的时间中减去时间即可:

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

Of course you can use these 2 lines outside the method too: 当然,您也可以在方法之外使用以下两行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM