C# 中的线程安全可破坏事件触发类
最近,我被要求实施一个课程作为选拔过程的一部分。我按照要求做了程序。然而,我考试失败了。我真的很想知道我的解决方案出了什么问题。非常感谢任何帮助。问题和我的解决方案如下
:
实现一个线程安全类,该类从构造开始每秒触发一个事件。需要有一个函数来查找经过的秒数。此类必须实现 IDisposable,并且在调用 dispose 后对秒数经过函数的任何调用都应该失败。
我的解决方案:
namespace TimeCounter
{
public delegate void SecondsElapsedHandler(object o, EventArgs e);
/// <summary>
/// Summary description for SecondCounter
/// </summary>
public class SecondCounter : IDisposable
{
private volatile int nSecondsElapsed;
Timer myTimer;
private readonly object EventLock = new object();
private SecondsElapsedHandler secondsHandler;
public SecondCounter()
{
nSecondsElapsed = 0;
myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler(OneSecondElapsed);
myTimer.Interval = 1000;
myTimer.AutoReset = false;
myTimer.Start();
}
public void OneSecondElapsed(object source, ElapsedEventArgs e)
{
try
{
SecondsElapsedHandler handlerCopy;
lock (EventLock)
{
handlerCopy = secondsHandler;
nSecondsElapsed++;
}
if (secondsHandler != null)
{
secondsHandler(this, e);
}
}
catch (Exception exp)
{
Console.WriteLine("Exception thrown from SecondCounter OneSecondElapsed " + exp.Message);
}
finally
{
if (myTimer != null)
{
myTimer.Enabled = true;
}
}
}
public event SecondsElapsedHandler AnotherSecondElapsed
{
add
{
lock (EventLock)
{
secondsHandler += value;
}
}
remove
{
lock (EventLock)
{
secondsHandler -= value;
}
}
}
public int SecondsElapsed()
{
if (this.IsDisposed)
{
throw new ObjectDisposedException("SecondCounter");
}
return nSecondsElapsed;
}
private bool IsDisposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool Disposing)
{
if (!IsDisposed)
{
if (Disposing)
{
}
if (myTimer != null)
{
myTimer.Dispose();
}
}
secondsHandler = null;
IsDisposed = true;
}
~SecondCounter()
{
Dispose(false);
}
}
}
Recently, I was asked to implement a class as part of a selection process. I did the program as requested. However, I failed in the test. I am really curious to know what is wrong in my solution. Any help is much appreciated. The question and my solution are given below
Question:
Implement a thread safe class which fires an event every second from construction. There need to be a function for finding the seconds elapsed. This class has to implement IDisposable and any calls to seconds elapsed function after calling dispose should fail.
My solution:
namespace TimeCounter
{
public delegate void SecondsElapsedHandler(object o, EventArgs e);
/// <summary>
/// Summary description for SecondCounter
/// </summary>
public class SecondCounter : IDisposable
{
private volatile int nSecondsElapsed;
Timer myTimer;
private readonly object EventLock = new object();
private SecondsElapsedHandler secondsHandler;
public SecondCounter()
{
nSecondsElapsed = 0;
myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler(OneSecondElapsed);
myTimer.Interval = 1000;
myTimer.AutoReset = false;
myTimer.Start();
}
public void OneSecondElapsed(object source, ElapsedEventArgs e)
{
try
{
SecondsElapsedHandler handlerCopy;
lock (EventLock)
{
handlerCopy = secondsHandler;
nSecondsElapsed++;
}
if (secondsHandler != null)
{
secondsHandler(this, e);
}
}
catch (Exception exp)
{
Console.WriteLine("Exception thrown from SecondCounter OneSecondElapsed " + exp.Message);
}
finally
{
if (myTimer != null)
{
myTimer.Enabled = true;
}
}
}
public event SecondsElapsedHandler AnotherSecondElapsed
{
add
{
lock (EventLock)
{
secondsHandler += value;
}
}
remove
{
lock (EventLock)
{
secondsHandler -= value;
}
}
}
public int SecondsElapsed()
{
if (this.IsDisposed)
{
throw new ObjectDisposedException("SecondCounter");
}
return nSecondsElapsed;
}
private bool IsDisposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool Disposing)
{
if (!IsDisposed)
{
if (Disposing)
{
}
if (myTimer != null)
{
myTimer.Dispose();
}
}
secondsHandler = null;
IsDisposed = true;
}
~SecondCounter()
{
Dispose(false);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
存在一些问题:
您可能会因一般的异常吞咽而受到惩罚,尽管这与线程问题没有具体相关。
您的计时器上存在竞争条件。处置,因为您可以在再次设置为启用之前处置计时器,从而导致异常。
您从未在 Dispose 中将 myTimer 设置为 null。
您正在从终结器 (disusing=false) 访问托管类 myTimer,这是一个坏主意。
没有必要显式地实现带有锁定的事件。委托是不可变的,添加/删除事件永远不会导致无效的委托状态,但如果在回调触发的同时添加/删除委托,则可能会出现竞争条件。如果您使用标准的“公共事件”声明,而没有显式支持私有委托,则将自动处理同步。
(小点)如果您要实现完整的 Dispose 模式,通常会将 Dispose(bool dispose) 方法标记为
protected virtual
,以便派生类可以挂钩到处置机制。更好的是,将您的类标记为sealed
,这样您就可以完全消除终结器。There are a few problems:
You might have been penalized for general Exception swallowing though that's not specifically related to threading issues.
There's a race condition on your timer.Dispose, as you could Dispose the timer before it is set Enabled again, resulting in an Exception.
You never set myTimer to null in Dispose.
You're accessing the managed class myTimer from the finalizer (disposing=false), which is a bad idea.
The explicit implementation of the event with locking is unnecessary. Delegates are immutable and adding/removing an event will never result in an invalid delegate state, though there can be race conditions if delegates are added/removed around the same time that the callback is fired. If you use the standard 'public event' declaration without an explicit backing private delegate, the synchronization will be handled automatically.
(minor point) If you're implementing the full Dispose pattern, it's customary to mark the Dispose(bool disposing) method as
protected virtual
, so that deriving classes can hook into the disposal mechanism. Better yet, mark your classsealed
and you can eliminate the finalizer entirely.您的终结器可能已损坏。它正确地将
false
作为Dispose
参数传递。这应该告诉Dispose(bool)
避免尝试处置其他托管对象。但在该方法中,您输入:因此,您忽略了
Diswriting
的值。这意味着当该对象可能已经被终结时(如果它有终结器,它可能会这样做),您可以从终结器线程调用计时器的Dispose
方法。终结器以不可预测的顺序运行。通常建议不要从终结器调用其他 GC 管理的对象。事实上,现在通常建议您不要编写终结器。问题没有要求你写一个!不幸的是,大多数关于 IDisposable 的教程也讨论了终结器。他们是不同的主题。
您还可以捕获通用异常基类
Exception
。这意味着您会捕获诸如NullReferenceException
之类的内容。通常不是一个好主意。您还可以登录控制台,这在 GUI 或基于服务器的应用程序中没有多大价值。您可以将: 替换
为:
您的变量命名不一致。请参阅Microsoft 指南。
Your finalizer is probably broken. It correctly passes
false
as theDisposing
parameter. This should tellDispose(bool)
to avoid attempting to dispose other managed objects. But in that method you put:So you ignore the value of
Disposing
. This means that you call the timer'sDispose
method from the finalizer thread, when that object may already have been finalized (if it has a finalizer, which it probably does). Finalizers run in an unpredictable order. It's generally recommended to not make calls to other GC-managed objects from a finalizer.In fact, it's usually recommended that you don't write finalizers at all these days. The question didn't ask you to write one! It's unfortunate that most tutorials about
IDisposable
also talk about finalizers. They're different subjects.You also catch
Exception
, the universal exception base class. This means you catch things likeNullReferenceException
. Not usually a good idea. You also log to the console, which is not worth much in a GUI or server-based application.You can replace:
with:
Your variable naming is inconsistent. Refer to the Microsoft guidelines.