-2
我是單元測試新手,剛開始學習它。這是我試圖測試的代碼。這是System.Threading.Timer的一個包裝,它不會泄漏異常。在此先感謝您的幫助。C#NUnit測試定時器
<pre>
using System;
using System.Threading;
namespace MCC.Test.Unit
{
public class MyTimer : IDisposable
{
private static readonly TimeSpan NoAutoStartTimer = TimeSpan.FromMilliseconds(-1);
private readonly Object _synclock = new object();
private Timer _internalTimer;
public MyTimer()
{
_internalTimer = new Timer(TimerTick);
}
public MyTimer(Action tickHandler) : this()
{
Tick += tickHandler;
}
public void Dispose()
{
if (_internalTimer != null)
{
try
{
lock (_synclock)
{
_internalTimer.Dispose();
_internalTimer = null;
foreach (Delegate d in Tick.GetInvocationList())
{
Tick -= (Action) d;
}
}
}
catch (Exception err)
{
Console.Error.WriteLine(err);
}
}
}
/// <summary>
/// Called when the timer ticks
/// </summary>
public event Action Tick;
private void TimerTick(Object state)
{
try
{
if (Tick != null)
{
Tick();
}
}
catch (Exception err)
{
Console.Error.WriteLine(err);
}
}
/// <summary>
/// Schedules the next tick
/// </summary>
/// <param name="next">The duration until the next sprint</param>
public void TriggerNextTickIn(TimeSpan next)
{
try
{
lock (_synclock)
{
if (_internalTimer != null)
{
_internalTimer.Change(next, NoAutoStartTimer);
}
}
}
catch (Exception err)
{
Console.Error.WriteLine(err);
}
}
}
}
</pre>
你的問題是什麼?您也可以結帳http://codereview.stackexchange.com/如果您只是在尋找代碼審查 –
問題是我如何檢查計時器是否打勾,謝謝 –