C# 即使在异常之后,任务也会继续执行

C# 即使在异常之后,任务也会继续执行,c#,task,task-parallel-library,C#,Task,Task Parallel Library,下面任务中的方法relatoriostaticos.abridatatual返回的异常已在方法本身中处理,问题是任务继续执行下一行var links=ListArquivos.ListaLinksDownlaod(驱动程序)这取决于要执行的方法abirdataatatual(),它还会引发异常。我曾尝试在方法中进行处理,将任务放入一个Try/catch中,但没有任何效果,方法listalinkDownlaod中总是存在异常,甚至不应该到达那里 如何停止任务的执行,例如当我们发送Cancellat

下面任务中的方法relatoriostaticos.abridatatual返回的异常已在方法本身中处理,问题是任务继续执行下一行var links=ListArquivos.ListaLinksDownlaod(驱动程序)这取决于要执行的方法abirdataatatual(),它还会引发异常。我曾尝试在方法中进行处理,将任务放入一个Try/catch中,但没有任何效果,方法listalinkDownlaod中总是存在异常,甚至不应该到达那里

如何停止任务的执行,例如当我们发送CancellationToken时,但这一次,当发生异常时

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;
    });
}
专用异步任务Acessar(IWebDriver驱动程序、字符串数据、CancellationToken ct)
{
返回等待任务。运行(()=>
{
ct.ThrowIfCancellationRequested();
LoginNgin.Login(config.User、config.Password、driver);
相对静态。ABR相对静态(驱动程序);
相关静态数据(驱动程序、数据);
var links=ListArquivos.ListaLinksDownlaod(驱动程序);
MethodInvoker action=delegate{pgbStatus.Maximum=links.Count();};
pgbStatus.BeginInvoke(动作);
返回链接;
});
}

如果看不到
AbirDatatual
的实际实现,就不可能确定地说,但是这个方法显然是在处理不应该在那里处理的异常

通常,方法应该只处理异常,如果它能够正确地处理它(正确的意思是它可以将应用程序恢复到程序可以安全继续的状态,通知用户错误等),否则它根本不应该处理它,让异常传播到方法的调用方

根据对您问题的描述,
abridatatual
不能(也不能)正确处理异常,因此您不应该在那里捕获异常(或者如果必须在那里捕获异常,您应该在那里捕获)。以下所有方法(包括
ListArquivos.listalinkDownlaod
)都将被跳过,直到处理异常为止。问题解决了

以下示例显示了如何直接在任务中处理异常(在
abridatatual
中删除异常处理后)。但它可能仍然不是此类异常处理程序的最佳位置,但再次强调,找到此类位置需要完整的源代码,因此请将其作为一个示例来澄清我所说的内容:

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);

        try
        {
            RelatoriosEstaticos.AbrirDataAtual(driver, data);

            var links = ListArquivos.ListaLinksDownlaod(driver);

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

            return links;
        }
        catch (Exception)//Use more specific exception type if possible
        {
            //Do all neccesary to properly handle the exception
        }
    });
}
bool AbrirDataAtual(IWebDriver driver, string data)
{
    try
    {
        //Do all the neccessary stuff
        ...

        //Indicate that AbrirDataAtual succeeded
        return true;
    }
    catch(Exception)
    {
        //Handle exception properly
        ...

        //Indicate that AbrirDataAtual failed
        return false;
    }
}

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);

        if (RelatoriosEstaticos.AbrirDataAtual(driver, data))
        {
            //Continue execution
            var links = ListArquivos.ListaLinksDownlaod(driver);

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

            return links;
        }
        else
        {
            //AbrirDataAtual failed
            return null;
            //or throw exception if appropriate
            throw new Exception();
        }
    });
}