简体   繁体   中英

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. regardless of how I start or run my task, or whether CLR exceptions are selected or not.

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.

Update 1: It appears that the breakpoints aren't being hit if a line of code that comes after???! 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. (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. (even if the program compiles happily)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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