我缺少反应式扩展计时器的什么? [英] What Am I Missing About Reactive Extension Timers?

查看:34
本文介绍了我缺少反应式扩展计时器的什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

watchers
  .ToObservable() // needs to be observable
  .SelectMany(watcher =>         // working on each watcher
    Observable
      // create a timer for the watcher
      .Timer(watcher.StartTime, TimeSpan.FromHours(watcher.Interval))  
      .SelectMany(Observable.FromAsync(
        async () => new { watcher, result = await CheckFolder(watcher.Path) }))) 
  .Subscribe(x => Console.WriteLine(string.Format("Watcher: {0}\tResult: {1}\tTime: {2}", x.watcher.Name, x.result, DateTimeOffset.Now))); // tell everyone what happened.

这篇文章中的一小段代码我开始走这条路.目标是在每次 Timer 发布时根据给定的开始时间和间隔 ping 网络服务(通过 CheckFolder() 方法).

Which is a nice little bit of code from this post that got me started down this road. The goal is to ping a web service (via the CheckFolder() method) every time the Timers publish, based on a given start time and interval.

问题是,每次我运行程序时,它都会为第一个 Watcher 输出一条消息,然后程序退出时没有错误.它得到第一个答案,它就完成了.

The trouble is, every time I run the program it outputs a single message for the first Watcher, and then the program exits without error. It gets the first answer, and it's finished.

如何让它等待所有计时器的其他发布?

How do get it to wait for the other publications from all the timers?

我几乎肯定我没有以正确的方式问这个问题,但希望一些反馈能帮助我完善我的问题.

I'm almost positive I'm not asking this question the right way, but hopefully a little feedback will help me refine my question.

谢谢.

推荐答案

这可能是因为 Subscribe 是一个非阻塞调用.IE.如果你有;

This is likely because Subscribe is a non-blocking call. I.e. if you have;

static void Main(string[] args)
{
  Observable.Timer(DateTimeOffset.Now, TimeSpan.FromSeconds(0.5))
            .Subscribe(x => Console.WriteLine("Got " + x));
}

你可能会发现它什么都不打印(或者可能得到 0",这取决于你的电脑感觉如何)

You'll probably find it prints nothing (or maybe "Got 0", depending on how your PC is feeling)

如果你通过等待按键被按下来阻止 Main 退出,就像这样:

If you stop Main from exiting, by waiting for a key to be pressed, like this:

static void Main(string[] args)
{
    Observable.Timer(DateTimeOffset.Now, TimeSpan.FromSeconds(0.5))
               .Subscribe(x => Console.WriteLine("Got " + x));
    Console.ReadKey();
}

然后它应该一直打印出值,直到你按下一个键.

Then it should keep printing out values until you press a key.

需要记住的是,激活订阅不足以保持您的程序运行.如果您正在编写具有某些 UI 的应用程序,那么您通常会有一个消息循环 - 您的程序将一直存在,直到您关闭窗口.但对于控制台应用程序而言并非如此,一旦您到达 main 的末尾,您的程序就结束了.

The thing to remember is that having an activating subscription, isn't enough to keep your programming running. If you're writing an application with some UI, then you'll usually have a message loop - which will your program alive until you close the window. But that isn't the case for console apps, once you get to the end of main, that's the end of your program.

因此,您需要找到一种方法来避免您的应用在阅读之前退出.等待按下特定键是一种常见的方法,因此可能对您有用.例如

So you need to find a way to avoid your app exiting before you're reading. Waiting for a specific key to be pressed is a common way to do it, so that may work for you. e.g.

static void Main(string[] args)
{
    Observable.Timer(DateTimeOffset.Now, TimeSpan.FromSeconds(0.5))
              .Subscribe(x => Console.WriteLine("Got " + x));

    while (Console.ReadKey().Key != ConsoleKey.Q)
    {
    }
}

这篇关于我缺少反应式扩展计时器的什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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