Task.Delay与DispatcherTimer?

编程入门 行业动态 更新时间:2024-10-25 02:31:09
本文介绍了Task.Delay与DispatcherTimer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在考虑将Task.Delay()用于不间断计时器,因为它更简单易读.

I'm considering use Task.Delay() for a non-stop timer, because it's more simple and readable.

由于我是.NET的新手,所以我认为这两个代码之间没有显着差异.您能告诉我它们之间的区别(如果有)吗?

As I'm new to .NET, I see no significant difference between the two codes. Can you show me the difference (if there is any) between them?

// Create variable at some place DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(5); timer.Tick += timer_Elapsed; timer.Start(); // Function other place void timer_Elapsed(object sender, EventArgs e) { //Do stuff }

vs

// Every thing inside a function async void TaskTimer() { while (true) { await Task.Delay(5000); // Do stuff } }

推荐答案

有两个主要区别:

  • Task.Delay方法将在个循环之间延迟指定的时间,而DispatcherTimer方法将在指定的循环时间开始一个新的循环.
  • Task.Delay具有更高的可移植性,因为它不依赖于绑定到特定UI的类型.
  • The Task.Delay approach will delay the specified amount of time between cycles, while the DispatcherTimer approach will start a new cycle on the specified cycle time.
  • Task.Delay is more portable, since it does not depend on a type tied to a specific UI.
  • 更多推荐

    Task.Delay与DispatcherTimer?

    本文发布于:2023-11-08 16:15:31,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1569858.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:Task   Delay   DispatcherTimer

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!