使用计时器等待过程完成 [英] Using a timer to wait for processes to complete

查看:100
本文介绍了使用计时器等待过程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下有两个按钮可以调用异步函数

Imagine there are two buttons that call an asynchronous function

  int packProcesses=0;  //the number of processes we are dealing with
    bool busy = false;  //are we busy?
   int v=10;
  private async void button5_Click(object sender, EventArgs e)
  {
    packProcesses++;
    busy = true;
    Trace.WriteLine("PROCESSES " + packProcesses + " busy? " + busy);
    //Do something
    var result = await DelayAndReturnAsync(v);
    //finished?
    packProcesses--;
    if (packProcesses <= 0) busy = false;
    Trace.WriteLine("Processes " + packProcesses + " busy? " + busy);
    }

    private async void button6_Click(object sender, EventArgs e)
     {
       packProcesses++;
       busy = true;
       Trace.WriteLine("PROCESSES " + packProcesses + " busy? " + busy);

        //Do something
        var result = await DelayAndReturnAsync(v);
        //finished?
        packProcesses--;
        if (packProcesses <= 0) busy = false;
        Trace.WriteLine("Processes " + packProcesses + " busy? " + busy);
        }

其中异步函数为

 async Task<int>DelayAndReturnAsync(int val)
  {
   await Task.Delay(TimeSpan.FromSeconds(val)).ConfigureAwait(false);
   Trace.WriteLine("Time" + DateTime.Now);
    return val;
   }

,我想有另一个调用这两个按钮的按钮. 如果我只是将两个点击功能一个接一个地放置,那么我将同时启动两个过程.

and I want to have another button that calls both of the buttons. If I just put both click functions one after another I will have both processes started at once.

因为我希望一个过程在另一个过程之后开始

Since I want one processes to start after the other I do

 private async void button8_Click(object sender, EventArgs e)
  {
  button5_Click(sender, e);
   do
    {
     await Task.Delay(1000);
     } while (busy);
    button6_Click(sender, e);
    }

我从此答案中获得了这个想法

I took the idea from this answer

这是个好主意吗?我不想阻塞CPU来执行此操作. 是否有更好的方法来等待一个过程完成以启动另一个过程?

Is this a good idea? I don't want to clog the CPU in order to do this. Is there a better way to wait for one process to complete to start the other?

推荐答案

您可以将逻辑从处理程序内部移至另一个方法:

You can move your logic from inside the handler to another method:

private async void button1_Click(object sender, EventArgs e)
{
    await Process1();
}

private async Task Process1()
{
    packProcesses++;
    busy = true;
    Trace.WriteLine("PROCESSES " + packProcesses + " busy? " + busy);
    //Do something
    var result = await DelayAndReturnAsync(v);
    //finished?
    packProcesses--;
    if (packProcesses <= 0) busy = false;
    Trace.WriteLine("Processes " + packProcesses + " busy? " + busy);
}

private async void button2_Click(object sender, EventArgs e)
{
    await Process2();
}

private async Task Process2()
{
    packProcesses++;
    busy = true;
    Trace.WriteLine("PROCESSES " + packProcesses + " busy? " + busy);

    //Do something
    var result = await DelayAndReturnAsync(v);
    //finished?
    packProcesses--;
    if (packProcesses <= 0) busy = false;
    Trace.WriteLine("Processes " + packProcesses + " busy? " + busy);
}

然后您可以等待它们:

private async void button3_Click(object sender, EventArgs e)
{
    await Process1();
    await Process2();
}

这篇关于使用计时器等待过程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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