WPF中的定时器类

Posted on August 06, 2007 by Fdream

刚开始接触WPF不久,也是第一次尝试开始写C#,写定时器的时候以为C#中System.Timer中的Timer类,结果发现老是抛出个很奇怪的异常:

The calling thread cannot access this object because a different thread owns it.

后来才发现,其实WPF是有自己的定时器类的,那就是System.Windows.Threading中的Timer类,使用起来也很简单:

//构造一个DispatcherTimer类实例
DispatcherTimer dTimer = new System.Windows.Threading.DispatcherTimer();

//设置事件处理函数
dTimer.Tick += new EventHandler(dTimer_Tick);

//定时器时间间隔1s
dTimer.Interval = new TimeSpan(0,0,1);

//启动定时器
dTimer.Start();

在上例中,也可以使用dTimer.IsEnabled = true;来启动定时器。

分享 |
Categories:
Silverlight/C#
Tags:
Comments:
5 Comments
Views:
22,675 Views

Related Posts

5 Responses to <WPF中的定时器类>

  1. HotHeart says:

    开始玩WPF了?

  2. Fdream says:

    嗯,接触三个星期了,WPF用来做应用程序确实不错,一切都可以动起来,呵呵~

  3. HotHeart says:

    我也想玩玩的说,不过貌似入门有点麻烦的,看了下微软的SilverLight入门教程,有点头大。
    动起来是什么意思?

  4. Fdream says:

    呵呵,涉及的东西比较多,如果不熟悉XAML的话可以用Blend来生成,结合生成的动画就能大致了解XAML了
    我说的动起来是指和一般的应用程序不一样,界面可以有很炫的动画效果,能够方便地和用户交互

  5. HotHeart says:

    本来是觉得Blend貌似还在beta的,就没去下了~~
    看来如果要玩WPF还是要下个Expression Studio[yuk]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>