异步友好DispatcherTimer包装/子 [英] Async friendly DispatcherTimer wrapper/subclass

查看:115
本文介绍了异步友好DispatcherTimer包装/子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的code一 DispatcherTimer 运行的火,每30秒更新从服务器系统的状态。定时器触发在客户端,即使我调试我的服务器code,所以如果我一直在调试5分钟,我可能会在客户端十几超时结束。最后决定我需要解决这一问题,以便寻求做出更异步 / 等待友好DispatcherTimer。

I have a DispatcherTimer running in my code that fire every 30 seconds to update system status from the server. The timer fires in the client even if I'm debugging my server code so if I've been debugging for 5 minutes I may end up with a dozen timeouts in the client. Finally decided I needed to fix this so looking to make a more async / await friendly DispatcherTimer.


  • code在 DispatcherTimer 运行必须是可配置无论是折返与否(即如果任务已经运行它不应该尝试再次运行)

  • 应基于任务的(这是否需要我实际上是在根暴露任务是一个灰色区域)

  • 应该可以运行异步code和等待上的任务完成

  • 无论是包装还是扩展DispatcherTimer可能并不真正的问题,但其包装可能会略有减少歧义,如果你不知道如何使用它

  • 可能暴露于 IsRunning 可绑定属性UI

  • Code running in DispatcherTimer must be configurable whether it is reentrant or not (i.e. if the task is already running it should not try to run it again)
  • Should be task based (whether or not this requires I actually expose Task at the root is a gray area)
  • Should be able to run async code and await on tasks to complete
  • Whether it wraps or extends DispatcherTimer probably doesn't really matter but wrapping it may be slightly less ambiguous if you don't know how to use it
  • Possibly expose bindable properties for IsRunning for UI

推荐答案

下面是我想出了。


  • SmartDispatcherTimer 扩展 DispatcherTimer (是为了得到这个启动和运行最简单的方法)

  • 有一个 TickTask 属性提供了工作来处理逻辑

  • 有一个 IsReentrant 属性(当然,整个的一点是,我希望它不会重入因此通常这是假的)

  • 它假定任何事情你打电话是完全awaitable - 或者你会最终失去了重入保护福利

  • SmartDispatcherTimer Extends DispatcherTimer (was easiest way to get this up and running)
  • Has a TickTask property to provide a Task to handle the logic
  • Has an IsReentrant property (of course the whole point is that I want it to not be reentrant so normally this is false)
  • It assumes anything you're calling is fully awaitable - or you'd end up losing the reentrancy protection benefits

用法:

        var timer = new SmartDispatcherTimer();
        timer.IsReentrant = false;
        timer.Interval = TimeSpan.FromSeconds(30);
        timer.TickTask = async () =>
        {
            StatusMessage = "Updating...";  // MVVM property
            await UpdateSystemStatus(false);
            StatusMessage = "Updated at " + DateTime.Now;
        };
        timer.Start();

这里的code。很想听听你对任何想法

Here's the code. Would love to hear any thoughts on it

public class SmartDispatcherTimer : DispatcherTimer
{
    public SmartDispatcherTimer()
    {
        base.Tick += SmartDispatcherTimer_Tick;
    }

    async void SmartDispatcherTimer_Tick(object sender, EventArgs e)
    {
        if (TickTask == null)
        {
            Debug.WriteLine("No task set!");
            return;
        }

        if (IsRunning && !IsReentrant)
        {
            // previous task hasn't completed
            Debug.WriteLine("Task already running");
            return;
        }

        try
        {
            // we're running it now
            IsRunning = true;

            Debug.WriteLine("Running Task");
            await TickTask.Invoke();
            Debug.WriteLine("Task Completed");
        }
        catch (Exception)
        {
            Debug.WriteLine("Task Failed");
        }
        finally
        {
            // allow it to run again
            IsRunning = false;
        }
    }

    public bool IsReentrant { get; set; }
    public bool IsRunning { get; private set; }

    public Func<Task> TickTask { get; set; }
}

这篇关于异步友好DispatcherTimer包装/子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆