The process or thread has changed since the last step (Visual Studio)

I have a C # .NET console application that sends an email using SmtpClient .

The following behavior appears in the code below:

  • Set a break point somewhere above line 35
  • Click the break point, go to lines 30, 32, etc.
  • Before he hits line 35, he will return to line 28 again
  • At some point, I get a "Process or thread has changed from the last step" message
  • Jumping seems random

The process or thread has changed since last step screen shot

Why is my break point jumping?

what does this message mean?

Is there something wrong with my code?

( https://stackoverflow.com/questions/958705/... Probably the same, but this is ASP.NET)


The following is the Pass Hans pointer.

This version still allows race status: the Toggle Timer.AutoReset property in my timer event handler to avoid re -entrancy while the code is running.

 private void OnTimerElapsed(object source, ElapsedEventArgs e) { timer.AutoReset = false; // prevent another Elapsed event MyClass.SendMail(smtpServer, account, password); // do stuff timer.AutoReset = true; // allow another Elapsed event } 

Final version: Initialize the timer with AutoReset already set to false, and then call Timer.Start() again at the end of the Elapsed event handler.

 public void Start() { timer = new Timer(checkInterval); timer.Elapsed += new ElapsedEventHandler(OnTimerElapsed); timer.AutoReset = false; // prevent race condition timer.Start(); } private void OnTimerElapsed(object source, ElapsedEventArgs e) { bool exceptionIsNasty = false; try { MyClass.SendMail(smtpServer, account, password); // do stuff } catch (Exception ex) { // Log exception, set exceptionIsNasty to true if the mailing should be stopped //... } finally { if (!exceptionIsNasty) timer.Start(); // allow another Elapsed event } } 
+7
source share
2 answers

You have more than one thread executing the same method, and they all fall into the same breakpoint. As you continue to walk, the debugger cannot accurately guess in which thread the state should be displayed. So you just get a warning that you are now looking at the state of another thread. The values โ€‹โ€‹of a local variable may be different. Of course, like the place of execution, the reason for the change in backlight.

Obviously, this can make debugging difficult. You want to avoid this, if you still get errors from the code, start only one thread. In addition, a temporary workaround is to use Debug + Windows + Threads, right-click one of the threads and select Freeze. Remember to defrost again.

+7
source

If you cannot start in one thread for debugging or if you do not want to change something for this to simplify debugging, you can also conditionally interrupt using the true filter. ThreadId == xxxxx (right click on a breakpoint).

You interrupt the method you need using a regular breakpoint, see which thread is current (there will be an arrow) in the threads window (Debug โ†’ Windows โ†’ Threads). The following breakpoints may be conditional, as I suggested.

note that on another start, the thread id will change and you will have to update it in the breakpoints window.

0
source

Source: https://habr.com/ru/post/958702/


All Articles