最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Debug.Assert(false) with no debugger attached - Stack Overflow

programmeradmin1浏览0评论

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
  • 2 Are you running as a Release build or as a Debug build? – mjwills Commented Mar 13 at 0:56
  • 2 learn.microsoft/en-us/dotnet/api/… – Hans Passant Commented Mar 13 at 1:47
  • Enable mixed-mode debugging. learn.microsoft/en-us/visualstudio/debugger/… – Gerry Schmitz Commented Mar 13 at 4:21
  • Mixed-mode debugging or the api link. This link is the reason Debug.Assert no longer shows a dialog box stackoverflow/questions/48413592/…. My question is simple enough - what are the alternatives? – Michael T Commented Mar 13 at 5:55
  • 1 Hans already posted a link to one: Call Debugger.Launch() - and one of the answers in the link you posted has a suggestion too. – Matthew Watson Commented Mar 13 at 9:35
Add a comment  | 

1 Answer 1

Reset to default 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); 
        }
发布评论

评论列表(0)

  1. 暂无评论