[英]Unable to Step into Async Task
我試圖通過大量類似的“如何調試異步任務”帖子和文章來讓它工作,但我仍然無法在我的 WPF 應用程序的異步方法中進入或獲取斷點來觸發。 無論我如何啟動或運行我的任務,或者是否選擇了 CLR 異常。
至於為什么要執行一項任務,我真的只希望我的 WPF 應用程序的 UI 在工作完成時保持響應,以便在應用程序思考時 UI 可用於排隊其他工作。
更新 1:如果后面有一行代碼,似乎斷點沒有被命中???! 斷點拋出異常。 它自身的異常只是一個 File.IO 異常(我將很快修復),其中有很多文件操作需要排序,其中一個作為請求的最小示例包括在內。 (異常不包含堆棧跟蹤以確定哪個特定行被拋出,而無需手動注釋掉它們,一次一個。)
具體來說,
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;
}
不是一個真正的答案,但我最接近讓斷點再次命中。
如果在任務線程的任何地方拋出異常,斷點似乎不會在任務/異步代碼上命中。 這個奇怪的行為調試器是由未處理的異常引起的,即使該異常發生在???! 斷點。
對於未來的搜索者,在清除所有未處理的異常之前,您可能無法使用 VS 調試器進入/通過您的任務代碼。 (即使程序編譯愉快)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.