如何优化算法? [英] How to optikmize algorith?
本文介绍了如何优化算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好。
我正在制作一个算法来检查设备是否启动。
这个想法是我做的计时器并初始化它,每隔1.5秒检查一次。
Hello.
I am making a algorithm that checks if a a device is started or not.
The idea is that I make timer and initialize it, checking every 1,5 sec.
private Timer timerChecker = new Timer();
timer.Tick += this.OnCheck();
private bool isStarted = true;
private bool isStopped = true;
private void OnCheck()
{
var process = Process.GetProcessByName("processname");
if(process.Lenght > 0)
{
if(!isStarted)
{
device.Start();//Start the device, sometimes it starts two times, which is not good
isStarted = true;
isStopped= false;
}
}
else
{
if(!isStopped)
{
device.Stop(); // Stop the device
isStarted = true;
isStopped= false;
}
}
}
能否为我提供更好的检查方式这个东西。
Can you offer me better way to check this thing.
推荐答案
首先,每次执行OnCheck时你应该禁用定时器,以避免在启动/弯腰时进行第二次调用,并在退出方法之前启用它。
First, every time it executes OnCheck you should disable the timer, to avoid a second call while starting/stooping, and enable it before exit the method.
private void OnCheck()
{
timerChecker.Enabled = false;
// Do things...
timerChecker.Enabled = true;
}
我不清楚当isStarted / isStoped改变它的价值时,我将其理解为MustStart& MustStop。如果没有,只需交替启动/停止该过程。
您只能为此功能使用一个变量。
It's not clear to me when isStarted/isStoped change it value, I understand it as MustStart & MustStop. If not, simply start/stop the process alternatively.
You could use one variable only for this function.
这篇关于如何优化算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文