Tag: 扩展方法

没有使用generics扩展方法的类型推断

我有以下方法: public static TEventInvocatorParameters Until (this TEventInvocatorParameters p, Func breakCond) where TEventInvocatorParameters : EventInvocatorParameters where TEventArgs : EventArgs { p.BreakCondition = breakCond; return p; } 而这堂课 public class EventInvocatorParameters where T : EventArgs { public Func BreakCondition { get; set; } // Other properties used below omitted for brevity. } 现在,我有以下问题: 此扩展方法显示所有类型,甚至是string 。 我不能写new EventInvocatorParameters(EventABC).Until(e […]

超时模式 – Thread.Abort真的有多糟糕?

我在各种网站上看到Thread.Abort不是很好用。 在这种情况下,如何实现超时模式? 例如,我已经读过MS在整个框架中使用下面的模式(我已经用扩展方法包装)。 就个人而言,我认为这是一个非常酷的扩展,但我担心Thread.Abort。 有没有人有更好的方法? public static bool CallandWait(this Action action, int timeout) { Thread subThread = null; Action wrappedAction = () => { subThread = Thread.CurrentThread; action(); }; IAsyncResult result = wrappedAction.BeginInvoke(null, null); if (((timeout != -1) && !result.IsCompleted) && (!result.AsyncWaitHandle.WaitOne(timeout, false) || !result.IsCompleted)) { if (subThread != null) { subThread.Abort(); } return false; […]