I have a C# app which is launched by another C++ exe.
The c# is a debug build.
When using .Net Framework 4.7 the Debug.Assert(false)
would always show a dialog giving me a chance to attach a debugger.
Now I'm using .NET 9 and the Debug.Assert(false)
no longer shows a dialog. So I don't get a chance to attach a debugger.
What is the best way to to pause my c# app so I can attach a debugger?
The options seem to be doing a Sleep
or showing a MessageBox OK, Cancel
This is what I have resorted to
#if DEBUG
MessageBox.Show("Attach debugger", "Title", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
#endif
Is there anything better?
I have a C# app which is launched by another C++ exe.
The c# is a debug build.
When using .Net Framework 4.7 the Debug.Assert(false)
would always show a dialog giving me a chance to attach a debugger.
Now I'm using .NET 9 and the Debug.Assert(false)
no longer shows a dialog. So I don't get a chance to attach a debugger.
What is the best way to to pause my c# app so I can attach a debugger?
The options seem to be doing a Sleep
or showing a MessageBox OK, Cancel
This is what I have resorted to
#if DEBUG
MessageBox.Show("Attach debugger", "Title", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
#endif
Is there anything better?
Share Improve this question edited Mar 14 at 1:52 Jack J Jun 6,0061 gold badge13 silver badges44 bronze badges asked Mar 13 at 0:55 Michael TMichael T 7918 silver badges23 bronze badges 5 |1 Answer
Reset to default 1As Hans' comment mentioned, it is feasible to use Debugger.Launch() and Debugger.Break().
Debugger.Launch() Forces the debugger to start if it is not already attached.
Debugger.Break() Triggers a breakpoint if a debugger is already attached.
Here is my code, you can refer to it:
static void Main()
{
#if DEBUG
if (!Debugger.IsAttached)
{
Debugger.Launch();
}
Debugger.Break();
#endif
Console.WriteLine("C# running");
Console.WriteLine("C# running1");
Console.WriteLine("C# running2");
Console.WriteLine("C# running3");
Thread.Sleep(5000);
}
Debugger.Launch()
- and one of the answers in the link you posted has a suggestion too. – Matthew Watson Commented Mar 13 at 9:35