zoukankan      html  css  js  c++  java
  • Timer in C# (2)

    System.Windows.Threading.DispatcherTimer

    A timer that is integrated into the Dispatcher queue which is processed at a specified interval of time and at a specified priority.

    The DispatcherTimer is reevaluated at the top of every Dispatcher loop.

    Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.

    If a System.Timers.Timer is used in a WPF application, it is worth noting that the System.Timers.Timer runs on a different thread than the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. For an example of using a System.Timers.Timer , see Disable Command Source via System Timer Sample. Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer.

    A DispatcherTimer will keep an object alive whenever the object's methods are bound to the timer.

     
    System.Windows.Threading.Dispatcher

    The Dispatcher maintains a prioritized queue of work items for a specific thread.

    When a Dispatcher is created on a thread, it becomes the only Dispatcher that can be associated with the thread, even if the Dispatcher is shut down.

    If you attempt to get the CurrentDispatcher for the current thread and a Dispatcher is not associated with the thread, a Dispatcher will be created.

    If a Dispatcher is shut down, it cannot be restarted.

    In WPF, a DispatcherObject can only be accessed by the Dispatcher it is associated with.  For example, a background thread cannot update the contents of a Button that is associated with the Dispatcher on the UI thread. In order for the background thread to access the Content property of the Button, the background thread must delegate the work to the Dispatcher associated with the UI thread. This is accomplished by using either Invoke or BeginInvoke.  Invoke is synchronous and BeginInvoke is asynchronous. The operation is added to the queue of the Dispatcher at the specified DispatcherPriority.

    If BeginInvoke is called on a Dispatcher that has shut down, the status property of the returned DispatcherOperation is set to Aborted.

    All of the methods on Dispatcher, with the exception of DisableProcessing, are free-threaded.

    Objects that derive from DispatcherObject have thread affinity.

    Objects that derive from Freezable are free-threaded when they are frozen. For more information, see Freezable Objects Overview.

    Remarks:

    1. Dispatcher的DisableProcessing方法只能由Dispatcher关联的线程调用,Dispatcher的其他方法都是free-threaded的。
    2. DispatcherObject 代表一个与Dispatcher相关的对象,他只能被创建它所关联的Dispatcher的线程访问,所以称之为thread affinity。
    3. 一个Freezable的对象有两种状态:unfrozen和frozen。一个unfrozen的Freezable 的Object还是可以被修改的,而一个frozen的object就不能被修改了,可以在多个线程间share,所以它是free-threaded。

    Ref:http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
    http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx

  • 相关阅读:
    解决VS打开文件出现No EditorOptionDefinition export found for the given option name问题
    git SourceTree 客户端 安装/使用教程
    C#调用WebService实例和开发
    Web Service 和WCF的比较
    Web Service 消息格式
    C#如何使用REST接口读写数据
    解决EasyNVR现场无固定公网IP的问题,万千企业期待的EasyNVS管理平台是什么?
    借助EasyNTS云组网,无需拉专线,也能解决设备现场无公网固定IP的问题
    EasyPlayer-RTSP-Android安卓播放器播放RTSP延迟优化策略,极低延时!
    EasyNVR现场部署搭配EasyNVS云端集中控制应用于幼儿园直播场景的最佳方案!
  • 原文地址:https://www.cnblogs.com/whyandinside/p/1541344.html
Copyright © 2011-2022 走看看