简体   繁体   English

在循环的每个迭代中等待任务

[英]await task in each iteration of a loop

What I want to do: 我想做的事:

I have a service with a timer that performs a certain sequence of actions every x seconds. 我有一个带有计时器的服务,该计时器每x秒执行一定的操作序列。 In some cases, one of the actions will have to be awaited (in the real life scenario, this is an answer from someone through a chat server). 在某些情况下,必须等待其中一项操作(在现实生活中,这是某人通过聊天服务器的答复)。

What I have tried below works, the only problem is that this piece of code: 我在下面尝试过的方法有效,唯一的问题是这段代码:

string result = await WaitAsynchronouslyAsync();

only gets awaited the first time, after the inital wait period, it just puts out one line every second (which is not what I want) 仅在第一次等待之后,在初始等待期之后,它每秒才放出一条线(这不是我想要的)

here's my code: 这是我的代码:

 static void Main(string[] args)
        {
            _timer.Start();
            _timer.Interval = 1000;
            _timer.Elapsed += _timer_Elapsed;
            _timer.Enabled = true;
            Console.Read();
        }

        static async void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //this has to be done every second, regardless of the await below              
            Console.WriteLine(DateTime.Now + " - " + "Current iteration: " + GetNumber++); 

            //this needs to be awaited everytime (in real life this is waiting for a response from a user)
            string result = await WaitAsynchronouslyAsync();
            Console.WriteLine(result);
        }

        public static  async Task<string> WaitAsynchronouslyAsync()
        {
            await Task.Delay(5000);
            var x = GetAsyncNumber ++;
            return x.ToString();
        }

Basically what I want is that the await inside the _timer_Elapsed method happens everytime (so every second it should wait for 5 seconds) and not just one time for 5 seconds, and then every second. 基本上,我想要的是_timer_Elapsed方法内的await每次都发生(因此每秒应等待5秒),而不仅仅是5秒一次,然后每秒一次。 I tried out some things with Task.WhenAll() but I don't really understand that and it also does not what I need. 我用Task.WhenAll()尝试了一些东西,但是我不太了解,它也不是我所需要的。

The _timer_Elapsed method gets invoked every second, and each time it will start a new asynchronous task in response to the WaitAsynchronouslyAsync() method call. _timer_Elapsed方法_timer_Elapsed被调用一次,并且每次它将启动新的异步任务以响应WaitAsynchronouslyAsync()方法调用时。 Since this async task takes 5 seconds to complete and a new async task is spawned every second, you will have up to 5 concurrent* tasks running at once. 由于此异步任务需要5秒钟才能完成,并且每秒都会产生一个新的异步任务,因此您一次最多可以运行5个并发*任务。 Then once the first async task completes (after 5 seconds) the second async task only has one second left before it completes. 然后,第一个异步任务完成后(5秒后),第二个异步任务仅剩一秒钟才完成。 And so on. 等等。

The key thing here is that you have multiple asynchronous tasks running at the same time, and the _timer_Elapsed method is always going to be called every second regardless of any asynchronous tasks that may be running from a previous invocation. 这里的关键是,您有多个异步任务同时运行,并且_timer_Elapsed方法总是每秒调用一次,而不管先前调用中正在运行的异步任务如何。 Also, _timer_Elapsed doesn't return a Task (and can't here anyway because it is an event handler) so the caller has no knowledge of any asynchronous tasks that may have started from inside the method. 另外, _timer_Elapsed不会返回Task (并且由于它是事件处理程序,因此在此也无法返回),因此调用方不知道可能从方法内部启动的任何异步任务。 The method returns before the task has completed. 该方法在任务完成之前返回。

*Note that these tasks may not necessarily be running on separate threads, so I'm using the word "concurrent" loosely here. *请注意,这些任务可能不一定在单独的线程上运行,因此在这里我宽松地使用“并发”一词。 See this for more information. 请参阅以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM