简体   繁体   中英

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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