How to ignore the passage of time while debugger has code paused?

后端 未结 3 607
时光取名叫无心
时光取名叫无心 2020-12-20 00:39

I have calculations I\'m performing which I need to timeout if it runs too long. I might set a timeout of 5 seconds, and do periodic polls throughout my code. Actual code

3条回答
  •  Happy的楠姐
    2020-12-20 01:13

    There is a not so nice workaround.

    You have to use Stopwatch instead of calculating the difference between present time and start time.

    Let's say you have the following piece of code:

    Stopwatch watch = Stopwatch.StartNew();
    
    DoSomething();
    DoAnotherThing(); <-- You want to add a breakpoint here
    DoFinalThings();
    
    watch.Stop();
    

    You can achieve what you want adding three lines of code before the line you want to breakpoint:

    Stopwatch watch = Stopwatch.StartNew();
    
    DoSomething();
    
    watch.Stop();       // Stops measuring time
    Debugger.Break();   // Programatically breaks the program at this point
    watch.Start();      // Resumes measuring time
    
    DoAnotherThing();
    DoFinalThings();
    
    watch.Stop();
    

提交回复
热议问题