関数の実行時間に基づいてタイマーを間隔に設定する方法。
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.
}
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();
//これにより、関数の実行時間に基づいてタイマー間隔が設定されます。}
最善の方法は、経過時間を正確に測定するために使用できる一連のメソッドとプロパティを提供するStopWatch クラスを使用することです。このようなもの:
// 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);