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

  • 相关阅读:
    12套有用的免费 PSD 格式 Android UI 素材
    使用 Canvas 和 JavaScript 创建逼真的下雨效果
    在网页设计中使用漂亮字体的16个优秀例子
    Koala – 开源的前端预处理器语言图形编译工具
    BackgroundCheck – 根据图片亮度智能切换元素样式
    经典网页设计:18个示例展示图片在网页中的使用
    TogetherJS – 酷!在网站中添加在线实时协作功能
    30个令人惊叹的灵感来自自然风光的网站设计《下篇》
    太有才了!创新的街头涂鸦手绘欣赏【中篇】
    15款美丽的设备模板,帮助展示你的 APP
  • 原文地址:https://www.cnblogs.com/whyandinside/p/1541344.html
Copyright © 2011-2022 走看看