开发者

WPF throw exception from worker thread but continue

开发者 https://www.devze.com 2023-02-06 21:03 出处:网络
I have a scenario where a worker thread performs a task that l开发者_如何学运维ooks like try { for()

I have a scenario where a worker thread performs a task that l开发者_如何学运维ooks like

try
{    
    for()
    {
        functionA();
        functionB(); 
    }
}
catch(Exception ex)
{
    throw new Exception("Message");    
}

I'm basically re-throwing the exception so that when the worker completes, the UI thread will receive the exception. Now, if an exception occurs in functionA I'd like to throw the exception but continue the loop and not stop. How can I achieve this?

edit: If it isn't clear, this is so that the UI can be notified that an exception has occurred but not stop the worker


Something like this:

List<Exception> errorsLog = new List<Exception>();

for()
{
    try
    {
       functionA();
    }
    catch(Exception e) {
       errorsLog.Add(e);
    }

    try
    {
       functionB();
    }
    catch(Exception e) {
       errorsLog.Add(e);
    }
}

if (errorsLog.Count > 0) {
   throw new AggregateException(errorsLog);
}

Where AggregateException is your custom exception that contains a list of other exceptions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号