2021-09-21 00:16:38 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace FDK
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>一定の間隔で処理を行うテンプレートパターンの定義。</para>
|
|
|
|
|
/// <para>たとえば、t進行() で 5ms ごとに行う処理を前回のt進行()の呼び出しから 15ms 後に呼び出した場合は、処理が 3回 実行される。</para>
|
|
|
|
|
/// </summary>
|
2023-09-18 01:11:17 +02:00
|
|
|
|
public class CIntervalProcessing : IDisposable
|
2021-09-21 00:16:38 +02:00
|
|
|
|
{
|
2023-09-18 01:11:17 +02:00
|
|
|
|
public delegate void dgProc();
|
|
|
|
|
public void Tick( long interval, dgProc proc )
|
2021-09-21 00:16:38 +02:00
|
|
|
|
{
|
|
|
|
|
// タイマ更新
|
|
|
|
|
|
|
|
|
|
if( this.timer == null )
|
|
|
|
|
return;
|
2023-09-18 01:11:17 +02:00
|
|
|
|
this.timer.Update();
|
2021-09-21 00:16:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初めての進行処理
|
|
|
|
|
|
2023-09-18 01:11:17 +02:00
|
|
|
|
if( this.PrevTime == CTimer.UnusedNum )
|
|
|
|
|
this.PrevTime = this.timer.NowTimeMs;
|
2021-09-21 00:16:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// タイマが一回りしてしまった時のため……
|
|
|
|
|
|
2023-09-18 01:11:17 +02:00
|
|
|
|
if( this.timer.NowTimeMs < this.PrevTime )
|
|
|
|
|
this.PrevTime = this.timer.NowTimeMs;
|
2021-09-21 00:16:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 時間内の処理を実行。
|
|
|
|
|
|
2023-09-18 01:11:17 +02:00
|
|
|
|
while( ( this.timer.NowTimeMs - this.PrevTime ) >= interval )
|
2021-09-21 00:16:38 +02:00
|
|
|
|
{
|
2023-09-18 01:11:17 +02:00
|
|
|
|
proc();
|
2021-09-21 00:16:38 +02:00
|
|
|
|
|
2023-09-18 01:11:17 +02:00
|
|
|
|
this.PrevTime += interval;
|
2021-09-21 00:16:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region [ IDisposable 実装 ]
|
|
|
|
|
//-----------------
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2023-09-18 01:11:17 +02:00
|
|
|
|
timer.Dispose();
|
2021-09-21 00:16:38 +02:00
|
|
|
|
}
|
|
|
|
|
//-----------------
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region [ protected ]
|
|
|
|
|
//-----------------
|
2023-09-18 01:11:17 +02:00
|
|
|
|
protected CTimer timer = new CTimer( CTimer.TimerType.MultiMedia );
|
|
|
|
|
protected long PrevTime = CTimer.UnusedNum;
|
2021-09-21 00:16:38 +02:00
|
|
|
|
//-----------------
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|