Task continuous execution even after exception

0

The method StaticReports.AreaData that is within the Task below is returning an exception already handled in the method itself, the problem is that the Task continues execution of the next line var links = ListLists.ListLinksDownlaod (driver); that depends on the OpenDataAnal method to execute, so it also throws an exception. I have tried to handle the method, put the task inside a Try / catch, but nothing works, there is always the exception in the ListLinksDownlaod method and it should not even get there.

How can I stop the execution of the task, such as 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;
    });
}
    
asked by anonymous 05.04.2018 / 17:45

1 answer

1

From what I understand, your problem is: you want to stop thread execution if any of the methods have an exception. The solution to this is: Your methods return a bool, which is set to true if you want to stop, and only execute the next thread method if that 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;
    }     
});
}
    
05.04.2018 / 18:08