-1

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?

5

1 Answer 1

1

As 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); 
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.