かなりユニークな問題があります。私が不在のときにヘッドレス ボックスで長時間実行されるアプリケーションがありますが、重要ではありません。Visual Studio を使用して、このアプリケーションをリモートでデバッグできるようにしたいと考えています。そうするために、次のようなコードがあります。
// Suspend all other threads to prevent loss
// of state while we investigate the issue.
SuspendAllButCurrentThread();
var remoteDebuggerProcess = new Process
{
StartInfo =
{
UseShellExecute = true,
FileName = MsVsMonPath;
}
};
// Exception handling and early return removed here for brevity.
remoteDebuggerProcess.Start();
// Wait for a debugger attach.
while (!Debugger.IsAttached)
{
Thread.Sleep(500);
}
Debugger.Break();
// Once we get here, we've hit continue in the debugger. Restore all of our threads,
// then get rid of the remote debugging tools.
ResumeAllButCurrentThread();
remoteDebuggerProcess.CloseMainWindow();
remoteDebuggerProcess.WaitForExit();
このように、離れている間にエラーが発生すると、アプリケーションは効果的に一時停止し、リモート デバッガーのアタッチを待機します。リモート デバッガーのアタッチは、最初の継続の後、Debugger.Break
呼び出しのおかげで適切なコンテキストを自動的に取得します。
ここに問題があります: 実装SuspendAllButCurrentThread
は自明ではありません。マネージド スレッドとネイティブ スレッドの間に 1 対 1 のマッピングがないため (現在のスレッドを維持する必要があるため)、Thread.Suspend
P/Invoke をダウンさせることはできません。SuspendThread
回避できる可能性がある場合、問題のマシンに Visual Studio をインストールしたくありません。どうすればこれを機能させることができますか?