Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I am experiences the following behavior in the code shown below:

  1. Set a break point somewhere above line 35
  2. Hit the break point, step through line 30, 32, etc.
  3. Before it hits line 35, it jumps back up to line 28 again
  4. At some point, I get the "The process or thread has changed since last step" message
  5. The jumping around seems random

The process or thread has changed since last step screen shot

Why does my break point jump around?

What does that message mean?

Is there anything wrong with my code?

(Here's another similar question on Stackoverflow. Probably the same thing, but it's ASP.NET)


Per Hans Passants's tip follows.

This version still permits race condition: Toggle Timer.AutoReset property in my timer's event handler to avoid re-entrancy while 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, then call Timer.Start() again at the end of your 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
   }
}
like image 280
JohnB Avatar asked Nov 23 '13 17:11

JohnB


People also ask

How do I stop a thread in Visual Studio?

Select a thread. Right-click the thread. Select "Kill thread" from the context menu.

How do I see running threads in Visual Studio?

To display the Threads window in break mode or run mode While Visual Studio is in debug mode, select the Debug menu, point to Windows, and then select Threads.


1 Answers

You've got more than one thread executing the same method and they all hit the same breakpoint. When you continue stepping, the debugger cannot accurately guess anymore which thread it should show the state for. So you just get a warning that you are now looking at the state of a different thread. The local variable values are liable to be different. As is the execution location of course, the reason the highlight changes.

Clearly this can make debugging more difficult. You want to avoid this if you are still getting bugs out of the code, start just one thread. Beyond that, a temporary workaround is to use Debug + Windows + Threads, right-click one of the threads and select Freeze. Don't forget to unfreeze again.

like image 71
Hans Passant Avatar answered Sep 21 '22 06:09

Hans Passant