+++ /dev/null
-using System;\r
-using System.Collections.Generic;\r
-using System.Text;\r
-using System.Runtime.InteropServices;\r
-using System.Diagnostics;\r
-using DirectShowLib;\r
-\r
-namespace FDK\r
-{\r
- public class CTimer : CTimerBase\r
- {\r
- public enum E種別\r
- {\r
- Unknown = -1,\r
- PerformanceCounter = 0,\r
- MultiMedia = 1,\r
- GetTickCount = 2,\r
- }\r
- public E種別 eタイマ種別\r
- {\r
- get;\r
- protected set;\r
- }\r
-\r
-\r
- public override long nシステム時刻ms\r
- {\r
- get\r
- {\r
- switch( this.eタイマ種別 )\r
- {\r
- case E種別.PerformanceCounter:\r
- {\r
- double num = 0.0;\r
- if( this.n現在の周波数 != 0L )\r
- {\r
- long x = 0L;\r
- QueryPerformanceCounter( ref x );\r
- num = ( (double) x ) / ( ( (double) this.n現在の周波数 ) / 1000.0 );\r
- }\r
- return (long) num;\r
- }\r
- case E種別.MultiMedia:\r
- return (long) timeGetTime();\r
-\r
- case E種別.GetTickCount:\r
- return (long) Environment.TickCount;\r
- }\r
- return 0;\r
- }\r
- }\r
-\r
- public CTimer( E種別 eタイマ種別 )\r
- :base()\r
- {\r
- this.eタイマ種別 = eタイマ種別;\r
-\r
- if( n参照カウント[ (int) this.eタイマ種別 ] == 0 )\r
- {\r
- switch( this.eタイマ種別 )\r
- {\r
- case E種別.PerformanceCounter:\r
- if( !this.b確認と設定_PerformanceCounter() && !this.b確認と設定_MultiMedia() )\r
- this.b確認と設定_GetTickCount();\r
- break;\r
-\r
- case E種別.MultiMedia:\r
- if( !this.b確認と設定_MultiMedia() && !this.b確認と設定_PerformanceCounter() )\r
- this.b確認と設定_GetTickCount();\r
- break;\r
-\r
- case E種別.GetTickCount:\r
- this.b確認と設定_GetTickCount();\r
- break;\r
-\r
- default:\r
- throw new ArgumentException( string.Format( "未知のタイマ種別です。[{0}]", this.eタイマ種別 ) );\r
- }\r
- }\r
- \r
- base.tリセット();\r
-\r
- n参照カウント[ (int) this.eタイマ種別 ]++;\r
- }\r
- \r
- public override void Dispose()\r
- {\r
- if( this.eタイマ種別 == E種別.Unknown )\r
- return;\r
-\r
- int type = (int) this.eタイマ種別;\r
-\r
- n参照カウント[ type ] = Math.Max( n参照カウント[ type ] - 1, 0 );\r
-\r
- if( n参照カウント[ type ] == 0 )\r
- {\r
- if( this.eタイマ種別 == E種別.MultiMedia )\r
- timeEndPeriod( this.timeCaps.wPeriodMin );\r
- }\r
-\r
- this.eタイマ種別 = E種別.Unknown;\r
- }\r
-\r
- #region [ protected ]\r
- //-----------------\r
- protected long n現在の周波数;\r
- protected static int[] n参照カウント = new int[ 3 ];\r
- protected TimeCaps timeCaps;\r
-\r
- protected bool b確認と設定_GetTickCount()\r
- {\r
- this.eタイマ種別 = E種別.GetTickCount;\r
- return true;\r
- }\r
- protected bool b確認と設定_MultiMedia()\r
- {\r
- this.timeCaps = new TimeCaps();\r
- if( ( timeGetDevCaps( out this.timeCaps, (uint) Marshal.SizeOf( typeof( TimeCaps ) ) ) == 0 ) && ( this.timeCaps.wPeriodMin < 10 ) )\r
- {\r
- this.eタイマ種別 = E種別.MultiMedia;\r
- timeBeginPeriod( this.timeCaps.wPeriodMin );\r
- return true;\r
- }\r
- return false;\r
- }\r
- protected bool b確認と設定_PerformanceCounter()\r
- {\r
- if( QueryPerformanceFrequency( ref this.n現在の周波数 ) != 0 )\r
- {\r
- this.eタイマ種別 = E種別.PerformanceCounter;\r
- return true;\r
- }\r
- return false;\r
- }\r
- //-----------------\r
- #endregion\r
-\r
- #region [ DllImport ]\r
- //-----------------\r
- [DllImport( "kernel32.dll" )]\r
- protected static extern short QueryPerformanceCounter( ref long x );\r
- [DllImport( "kernel32.dll" )]\r
- protected static extern short QueryPerformanceFrequency( ref long x );\r
- [DllImport( "winmm.dll" )]\r
- protected static extern void timeBeginPeriod( uint x );\r
- [DllImport( "winmm.dll" )]\r
- protected static extern void timeEndPeriod( uint x );\r
- [DllImport( "winmm.dll" )]\r
- protected static extern uint timeGetDevCaps( out TimeCaps timeCaps, uint size );\r
- [DllImport( "winmm.dll" )]\r
- protected static extern uint timeGetTime();\r
-\r
- [StructLayout( LayoutKind.Sequential )]\r
- protected struct TimeCaps\r
- {\r
- public uint wPeriodMin;\r
- public uint wPeriodMax;\r
- }\r
- //-----------------\r
- #endregion\r
- }\r
-}\r