简体   繁体   English

无法进入异步任务

[英]Unable to Step into Async Task

I've tried to get this to work via the multitude of similar "How to debug async tasks" posts and articles, but am still unable to step into or get a breakpoint to trigger in my WPF app's Async methods.我试图通过大量类似的“如何调试异步任务”帖子和文章来让它工作,但我仍然无法在我的 WPF 应用程序的异步方法中进入或获取断点来触发。 regardless of how I start or run my task, or whether CLR exceptions are selected or not.无论我如何启动或运行我的任务,或者是否选择了 CLR 异常。

As for why a task, I really only want my WPF app's UI to remain responsive while the work is being done so that the UI can be used to queued other work while the app is thinking.至于为什么要执行一项任务,我真的只希望我的 WPF 应用程序的 UI 在工作完成时保持响应,以便在应用程序思考时 UI 可用于排队其他工作。

Update 1: It appears that the breakpoints aren't being hit if a line of code that comes after???!更新 1:如果后面有一行代码,似乎断点没有被命中???! the breakpoint throws an exception.断点抛出异常。 The exception it's self is just a File.IO exception (that I'll fix shortly) of which there were a quite a few file operations to sort through, one included as the requested minimum example.它自身的异常只是一个 File.IO 异常(我将很快修复),其中有很多文件操作需要排序,其中一个作为请求的最小示例包括在内。 (The exception doesn't contain a stack trace to determine which specific line is throws it without manually commenting them out, one at a time.) (异常不包含堆栈跟踪以确定哪个特定行被抛出,而无需手动注释掉它们,一次一个。)

Specifically,具体来说,

const string APP_REG_NAMESPACE = "[Redacted]";

IProgress<int> progress;
IProgress<string> status;

public MainWindow() {
    InitializeComponent();
    this.DataContext = this;

    //Allows UI to remain responsive while work is being done.
    progress = new Progress<int>(UpdateProgress);
    status = new Progress<string>(UpdateStatus);
    
    //exceptions only appear here.
    Task.Run(() => EvalConfig(progress, status));
}

/*Removed as seemed un-needed
async Task Async_EvalConfig() {
    progress = new Progress<int>(UpdateProgress);
    status = new Progress<string>(UpdateStatus);

    //exceptions all thrown here.
    await Task.Run(() => EvalConfig(progress, status));
}*/

//This allows breakpoints to be hit on any of the 3 lines
void EvalConfig(IProgress<int> progress, IProgress<string> status) {
    //Give system a second (or 10)
    Task.Delay(5000);
    Thread.Sleep(5000);
    return;
}

//No breakpoints are ever hit, if an exception occurs anywhere, 
//even if on only the last line. 
//Exceptions are only shown at the line that actually runs the task.
void EvalConfig(IProgress<int> progress, IProgress<string> status) {
    //Give system a second
    await Task.Delay(500);
    cancellationTokenSource = new CancellationTokenSource();

    //Some long duration IO work done here to prepare data load.
    //[Redacted for brevity]

    //Check the registry for the protocol key
    if(TryGetProtocol(out string protocol)){
        //Use the protocol to connect and start doing some painfully slow work
        //[Truncated for brevity]
    }else{
        //Handle missing protocol logging and schedule for admin repair.
        //[Truncated for brevity]
    }
}

bool TryGetProtocol(out string strProtocol) {
    strProtocol = "";
    try {
        using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(APP_REG_NAMESPACE)) {
            if (key == null) {
                return false;
            } else {
                Object o = key.GetValue("");//AKA (Default)
                if (o == null) {
                    return false;
                }

                strProtocol = (o as String);
                if (string.IsNullOrEmpty(strProtocol)) {
                    return false;
                }
            }
        }
    //Oddly this didn't actually catch the error 
    //despite being super generic.
    } catch (Exception ex) {
        //Handle notification of exception.
        //[Truncated for brevity]
        return false;
    }

    return true;
}

Not really an answer, but the closest I came to getting breakpoints to hit again.不是一个真正的答案,但我最接近让断点再次命中。

It appears that the breakpoints won't be hit on Tasks/Async Code if an exception is thrown anywhere in the task's thread.如果在任务线程的任何地方抛出异常,断点似乎不会在任务/异步代码上命中。 This strange behavior debugger was caused by an unhandled exception even though that exception occurred after???!这个奇怪的行为调试器是由未处理的异常引起的,即使该异常发生在???! the breakpoint.断点。

To future searchers, you may not be able to step into/through your Task code with the VS debugger until you clear all unhandled exceptions.对于未来的搜索者,在清除所有未处理的异常之前,您可能无法使用 VS 调试器进入/通过您的任务代码。 (even if the program compiles happily) (即使程序编译愉快)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM