• 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

  • 相关阅读:
    Eular质数筛法
    质数测试
    求树的直径
    常用排序的实现方法(数据结构)
    关于整数的整数因子和问题的若干研究(数学)
    状态压缩中常用的位运算(DP)
    舞蹈链--求精密覆盖(数据结构)
    高斯消元模板,整数(数学)
    树状数组 (数据结构)
    二叉树还原--通用类型模板类(数据结构)
  • 原文地址:https://www.cnblogs.com/whyandinside/p/1541344.html
Copyright © 2020-2023  润新知