Last active
May 9, 2021 03:34
-
-
Save cointoss1973/db283b416d49c229c11f5740cbec5fad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Timers; | |
namespace ElapsedTimer | |
{ | |
public class ElapsedTimer | |
{ | |
public delegate void ElapsedTimeEventHandler(object sender, ElapsedTimeEventArgs e); | |
public event ElapsedTimeEventHandler ElapsedTimeEvent; | |
private Timer timer; | |
private Stopwatch sw; | |
/// <summary> | |
/// タイマー開始 (ミリ秒でイベント発行するインターバルを設定してください) | |
/// </summary> | |
/// <param name="intervalMillisecounds">イベント発行するインターバル(ミリ秒)</param> | |
public void Start(double intervalMillisecounds) | |
{ | |
timer = new System.Timers.Timer(); | |
timer.Interval = intervalMillisecounds; | |
timer.Elapsed += OnElapsedEventHandler; | |
timer.Start(); | |
sw = new Stopwatch(); | |
sw.Start(); | |
} | |
public void Stop() | |
{ | |
timer?.Stop(); | |
sw?.Stop(); | |
} | |
public void Restart() | |
{ | |
timer?.Stop(); | |
timer?.Start(); | |
sw?.Restart(); | |
} | |
private void OnElapsedEventHandler(object sender, System.Timers.ElapsedEventArgs e) | |
{ | |
ElapsedTimeEvent.Invoke(this, new ElapsedTimeEventArgs(e.SignalTime, this.sw.Elapsed)); | |
} | |
} | |
public class ElapsedTimeEventArgs : EventArgs | |
{ | |
/// <summary> | |
/// イベント発行時の時刻 | |
/// </summary> | |
public DateTime SignalTime { get; } | |
/// <summary> | |
/// 経過時間 | |
/// </summary> | |
public TimeSpan ElapsedTime { get; } | |
/// <summary> | |
/// コンストラクタ | |
/// </summary> | |
/// <param name="signal"></param> | |
/// <param name="elapsed"></param> | |
public ElapsedTimeEventArgs(DateTime signal, TimeSpan elapsed) | |
{ | |
SignalTime = signal; | |
ElapsedTime = elapsed; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ElapsedTimer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var et = new ElapsedTimer(); | |
et.ElapsedTimeEvent += OnElapsedTimeEvent; | |
double interval = 200; | |
et.Start(interval); | |
while(true) | |
{ | |
Console.WriteLine($"{interval}ms 毎にイベントが発行されます。なにかキーを押すとタイマーをリスタートします"); | |
Console.ReadKey(); | |
Console.WriteLine(Environment.NewLine); | |
et.Restart(); | |
} | |
} | |
static void OnElapsedTimeEvent(object sender, ElapsedTimeEventArgs e) | |
{ | |
string elapsedTime = $"{e.ElapsedTime.Hours:00}:{e.ElapsedTime.Minutes:00}:{e.ElapsedTime.Seconds:00}.{e.ElapsedTime.Milliseconds / 10:00}"; | |
Console.CursorVisible = false; | |
Console.Write($"イベント発行時刻 {e.SignalTime:hh:mm:ss.ff} 経過時間 {elapsedTime}"); | |
Console.SetCursorPosition(0, Console.CursorTop); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment