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

  • 相关阅读:
    maven安装以及eclipse配置maven
    jquery 图片文件转base64 显示
    Java 解析Excel文件为JSON
    Dropwizard框架入门
    使用Spring Security和OAuth2实现RESTful服务安全认证
    SQL语句大小写是否区分的问题,批量修改整个数据库所有表所有字段大小写
    13个可实现超棒数据可视化效果的Javascript框架
    C#创建数字证书并导出为pfx,并使用pfx进行非对称加解密
    C#使用RSA证书文件加密和解密示例
    C# 中使用 RSA加解密算法
  • 原文地址:https://www.cnblogs.com/whyandinside/p/1541344.html
Copyright © 2011-2022 走看看