-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/如果您只是在寻找代码审查 –
问题是我如何检查计时器是否打勾,谢谢 –