.net Timer的缺点

1 依赖QCall

.net3个Timer,form.timer会占用ui线程,更重要的是非winform程序你会去引用form吗,所以一般不用。threading中的timer提供的方法不好用,一般也不用,常用的是System.Timer.Timer

System.Timer.Timer实现上是依赖threading中的timer,而threading的timer又依赖TimerQueueTimer,最终是Qcall中的方法。再也反编译不到代码了,看不到具体如何实现的。

Timer是很常用功能,往往这种常用的其实很难实现,即使实现了还可能一堆缺点。这种不知根知底的东西还是尽量少用吧。

 

http://stackoverflow.com/questions/9491337/what-is-dllimportqcall

QCalls are calls to native methods within the CLR runtime. They behave like other [DllImport]s, but they're faster because they make specific (undocumented) assumptions about what the native methods do, so they can skip various marshalling and GC and exception checks.

 

threading的timer又依赖TimerQueueTimer

[SecurityCritical]
    private void TimerSetup(TimerCallback callback, object state, uint dueTime, uint period, ref StackCrawlMark stackMark)
    {
      if (callback == null)
        throw new ArgumentNullException("TimerCallback");
      this.m_timer = new TimerHolder(new TimerQueueTimer(callback, state, dueTime, period, ref stackMark));
    }

TimerQueue依赖QCall

[SuppressUnmanagedCodeSecurity]
    [SecurityCritical]
    [DllImport("QCall", CharSet = CharSet.Unicode)]
    private static bool ChangeAppDomainTimer(TimerQueue.AppDomainTimerSafeHandle handle, uint dueTime);

 

2 在Threadpool中运行,异常不能被外界捕获。

试过 AppDomain.CurrentDomain.UnhandledException和 TaskScheduler.UnobservedTaskException,都不能捕获。还有个Application.ThreadException只有form中可用没试过。

 

posted @ 2013-08-09 16:17  ^^!  阅读(709)  评论(0编辑  收藏  举报