How to check if there are async processes running

1

Does anyone know of any way to check if a Async process is running?

For example, I would like to do a check when the user closes the system, and do not close until all Async processes are complete.

async public void EvokeMetodo(string contrato, int item,  bool editar)
    {
        //retorna um datatable contendo o iten que foi salvo ou editado
        DataTable Ret_Contrato = await Ret_Datatable(contrato, item);
        int verificador = 0;
        //insert
        if (!editar)
        {
            //retorna o numero de linhas afetadas, ou seja, o numero de insert feitos
            verificador = await Insert_ContratoItens(Ret_Contrato);

            if(verificador > 0)
            {
                atualiza_ContratoWeb(contrato, item);
            }
        }
        else//update
        {
            verificador = await Update_ContratoItens(Ret_Contrato);

            if (verificador > 0)
            {
                atualiza_ContratoWeb(contrato, item);
            }
        }
    }
    
asked by anonymous 09.08.2017 / 20:04

2 answers

3

The correct and most effective way to check this is through Tasks .

1. Turn your method into Task

Let's turn your method into a Task so you can use the await directive, if you'd like to wait for your execute method to proceed.

private async Task EvokeMetodo(string contrato, int item,  bool editar)
{
    //retorna um datatable contendo o iten que foi salvo ou editado
    DataTable Ret_Contrato = await Ret_Datatable(contrato, item);
    int verificador = 0;
    //insert
    if (!editar)
    {
        //retorna o numero de linhas afetadas, ou seja, o numero de insert feitos
        verificador = await Insert_ContratoItens(Ret_Contrato);

        if(verificador > 0)
        {
            atualiza_ContratoWeb(contrato, item);
        }
    }
    else//update
    {
        verificador = await Update_ContratoItens(Ret_Contrato);

        if (verificador > 0)
        {
            atualiza_ContratoWeb(contrato, item);
        }
    }
}

Can not use await in void , so we create Task . Note that this Task is not returning anything, it's like a void speaking roughly, because it will just run. If you want the method to return something, just switch from Task to Task<int> and it will need you to return a int , for example.

2. Run your Task

Here we will execute your Task (method) and store the execution in a Task variable so that you can check its status later. In this example, I put the execution of your method on a Click event of a button. Note that the Click method of the button has the async directive.

private async void button1_Click(object sender, EventArgs e)
{
   Task task = Task.Run(async () =>
   {
       await EvokeMetodo();
   });
}

Since I used await within Task.Run to wait for the EvokeMetodo method to execute, it is necessary to use a lambda expression with the async() directive to "invoke" your method and inform the Task.Run you want wait for the execution of the method it will execute. More specifically in this part:

Task.Run(async () =>
{
   await EvokeMetodo();
});

3. Checking the status of your Task

If you noticed earlier, we stored the execution (return) of the Task.Run method on a variable of type Task called task . Through this, we can check the status of Task .

if(task.IsCompleted)
{
   // Faça alguma coisa
}

In addition to task.IsCompleted we also have task.IsCanceled and task.IsFaulted .

    
10.08.2017 / 13:59
0

I think this will solve, create a global variable with async name, and in your async code put

asyncRodando = true;
// CÓDIGO ASYNC AQUI
asyncRodando = false;

And at closing time do something like.

{
if(asyncRodando)
return;

fecharPrograma();

}
    
09.08.2017 / 20:28