Continuing task execution even after exception

Asked

Viewed 51 times

0

The method RelatoriosEstaticos.AbrirDataAtual which is within the Task below is returning an exception already dealt with in the method itself, the problem is that the Task continues the execution of the next line var links = Filelist.Listsdownlaod(driver); which depends on the method Startingcurrent to be executed, with this, she also throws an exception. I have tried to deal within the method, put the task inside a try/catch, but nothing works, always occurs the exception in the method Listsdownlaod and you shouldn’t even get there.

How can I interrupt the execution of the task, as it happens when we send a Cancellationtoken, but this time when any exception occurs.

private async Task<List<IWebElement>> Acessar(IWebDriver driver, string data, CancellationToken ct)
{
    return await Task.Run(() =>
    {
        ct.ThrowIfCancellationRequested();

        LoginNgin.Login(config.User, config.Password, driver);

        RelatoriosEstaticos.AbrirRelatoriosEstaticos(driver);

        RelatoriosEstaticos.AbrirDataAtual(driver, data);

        var links = ListArquivos.ListaLinksDownlaod(driver);

        MethodInvoker action = delegate { pgbStatus.Maximum = links.Count(); };
        pgbStatus.BeginInvoke(action);

        return links;
    });
}
  • Are you debugging right to understand what is really going on? and why are the exceptions not being dealt with? apparently the logic of your code isn’t making much sense.

  • Yeah, I debugged the code. as I said, an exception is being cleared in the current Abrir() method; this exception is already properly dealt with within the method, the problem is that even with this exception, the task ignores and continues the execution of the code.

1 answer

1


From what I understand, your problem is: you want to stop thread execution if within any of the methods there is an exception. The solution to this is: Your methods return a bool, which is set to true if you want to stop, and only executes the next thread method if this variable is false.

Example:

private async Task<List<IWebElement>> Acessar(IWebDriver driver, string data, CancellationToken ct)
{
    return await Task.Run(() =>
    {
        bool stop = false;

    ct.ThrowIfCancellationRequested();

    LoginNgin.Login(config.User, config.Password, driver);

    stop = RelatoriosEstaticos.AbrirRelatoriosEstaticos(driver);

    if (!stop)
        stop = RelatoriosEstaticos.AbrirDataAtual(driver, data);

    if (!stop)
    {
        var links = ListArquivos.ListaLinksDownlaod(driver);

        MethodInvoker action = delegate { pgbStatus.Maximum = links.Count();};
        pgbStatus.BeginInvoke(action);

        return links;
    }     
});
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.