使用 TPL 时如何在 catch 块处命中断点

发布于 2024-12-01 20:41:55 字数 2432 浏览 0 评论 0原文


当我开始通过 TPL 来理解时。我被这段代码困住了。我有2个任务。 Task1 抛出 ArgumentOutOfRangeException,Task2 抛出 NullReferenceException。

考虑下面的代码:

static void Main(string[] args) {

            // create the cancellation token source and the token
            CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;

            // create a task that waits on the cancellation token
            Task task1 = new Task(() => {
                // wait forever or until the token is cancelled
                token.WaitHandle.WaitOne(-1);
                // throw an exception to acknowledge the cancellation
                throw new OperationCanceledException(token);
            }, token);

            // create a task that throws an exceptiono
            Task task2 = new Task(() => {
                throw new NullReferenceException();
            });

            // start the tasks
            task1.Start(); task2.Start();

            // cancel the token
            tokenSource.Cancel();

            // wait on the tasks and catch any exceptions
            try {
                Task.WaitAll(task1, task2);
            } catch (AggregateException ex) {
                // iterate through the inner exceptions using 
                // the handle method
                ex.Handle((inner) => {
                    if (inner is OperationCanceledException) {
                        // ...handle task cancellation...
                        return true;
                    } else {
                        // this is an exception we don't know how
                        // to handle, so return false
                        return false;
                    }
                });
            }

            // wait for input before exiting
            Console.WriteLine("Main method complete. Press enter to finish.");
            Console.ReadLine();
        }

我已经为 Task.WaitAll(task1, task2) 放置了 try catch 块。理想情况下,它应该命中 Catch 块内 ex.handler 语句中的断点。据我了解,无论结果是什么,它都应该击中 catch 块。

如果我有task1.Result/task2.Result,也会发生同样的情况。

我的问题是:在调试模式下,当我故意将其从任务中抛出时,为什么在 catch 块处没有命中断点,因为我想检查 catch 块下的语句。它只是在“用户代码未处理的 NullReferenceException”处添加黄色标记。

Task task2 = new Task(() => {
                throw new NullReferenceException();
            });

如何在 catch 块处打断点???

感谢您的回复:)

As I started understanding thru the TPL. I got stuck over in this code. I have 2 task. Task1 thows ArgumentOutOfRangeException and Task2 throws NullReferenceException.

Consider this below code:

static void Main(string[] args) {

            // create the cancellation token source and the token
            CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;

            // create a task that waits on the cancellation token
            Task task1 = new Task(() => {
                // wait forever or until the token is cancelled
                token.WaitHandle.WaitOne(-1);
                // throw an exception to acknowledge the cancellation
                throw new OperationCanceledException(token);
            }, token);

            // create a task that throws an exceptiono
            Task task2 = new Task(() => {
                throw new NullReferenceException();
            });

            // start the tasks
            task1.Start(); task2.Start();

            // cancel the token
            tokenSource.Cancel();

            // wait on the tasks and catch any exceptions
            try {
                Task.WaitAll(task1, task2);
            } catch (AggregateException ex) {
                // iterate through the inner exceptions using 
                // the handle method
                ex.Handle((inner) => {
                    if (inner is OperationCanceledException) {
                        // ...handle task cancellation...
                        return true;
                    } else {
                        // this is an exception we don't know how
                        // to handle, so return false
                        return false;
                    }
                });
            }

            // wait for input before exiting
            Console.WriteLine("Main method complete. Press enter to finish.");
            Console.ReadLine();
        }

I have put the try catch block for Task.WaitAll(task1, task2). It should ideally hit the breakpoint in ex.handler statement inside Catch block. As I understand that whatever may be the result it should hit the catch block.

Same case is happening if I have task1.Result/task2.Result.

My Question is: In debug mode why isn't the breakpoint being hit at the catch block when I am intentionally throwing it from task as I want to examine the statements under catch block. It just puts yellow mark at saying "NullReferenceException unhandled by the user code".

Task task2 = new Task(() => {
                throw new NullReferenceException();
            });

How do I hit the break point at catch block???

Thanks for replying :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素染倾城色 2024-12-08 20:41:55

正如 Arne Claassen 在他们的评论中解释的那样,调试器在抛出原始异常时暂停执行,因为线程确实不处理异常。如果您继续执行(F5播放按钮),程序应继续执行到您在继续中处理异常的位置。

As Arne Claassen explained in their comment, the debugger pauses execution at the point the original exception is thrown because the thread does not handle the exception. If you continue exceution (F5 or play button), the program should continue to the point where you are handling the exception in your continuation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文