.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中可用没试过。

 

转载于:https://www.cnblogs.com/devourer/p/3248541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值