如何根据功能的执行时间将计时器设置为间隔。
int main()
{
Timer t=new Timer();
t.start();
myfunc();
t.stop();
long elapsed=stop-start;
Need to set timer to this interval everytime instead of fixed interval.
}
答案 0 :(得分:0)
最好的方法是使用StopWatch Class,它提供了一组方法和属性,可用于准确测量经过的时间。有点像这样:
// Create new stopwatch
Stopwatch stopwatch = new Stopwatch();
// Begin timing
stopwatch.Start();
// Do something
myfunc();
// Stop timing
stopwatch.Stop();
// Write result
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
答案 1 :(得分:0)
class Myclass
{
private static Timer newTimer;
static void main()
{
newTimer=new Timer(timercallback,null,0,10000);
}
private static timercallback(Object o)
{
Stopwatch sw=new Stopwatch();
sw.Start();
myfunction();
sw.Stop();
newTimer.change(0,sw.ElapsedMilliseconds);
GC.Collect();
//这将根据函数执行时间设置定时器间隔。 }