Does a method execute on the same thread for its entire length?
-
I'm logging what thread my event handler is running on. My original log statement looks like this:
public void myEventHandler()
{
AnotherMethodWhereWorkIsDone(data);System.Diagnostics.Debugger.Log(0,"", "Current thread is: " + Threading.CurrentThread.GetHashCode());
}
However, it occurs to me that this will actually report the thread that
Debugger.Log
is running on, and not that of my handler. So then I snapshotted the CurrentThread.GetHashCode() at the top of my method.public void myEventHandler()
{
int currHashCode = Threading.CurrentThread.GetHashCode();AnotherMethodWhereWorkIsDone(data); System.Diagnostics.Debugger.Log(0,"", "Current thread is: " + currHashCode);
}
But now it occurs to me that I have no idea if my method is guaranteed to be executing on the same thread all the way through. Is there some chance that after
AnotherMethodWhereWorkIsDone()
executes, my handler will be running on a different thread than when I shapshotted it aboveAnotherMethodWhereWorkIsDone()
? -
I'm logging what thread my event handler is running on. My original log statement looks like this:
public void myEventHandler()
{
AnotherMethodWhereWorkIsDone(data);System.Diagnostics.Debugger.Log(0,"", "Current thread is: " + Threading.CurrentThread.GetHashCode());
}
However, it occurs to me that this will actually report the thread that
Debugger.Log
is running on, and not that of my handler. So then I snapshotted the CurrentThread.GetHashCode() at the top of my method.public void myEventHandler()
{
int currHashCode = Threading.CurrentThread.GetHashCode();AnotherMethodWhereWorkIsDone(data); System.Diagnostics.Debugger.Log(0,"", "Current thread is: " + currHashCode);
}
But now it occurs to me that I have no idea if my method is guaranteed to be executing on the same thread all the way through. Is there some chance that after
AnotherMethodWhereWorkIsDone()
executes, my handler will be running on a different thread than when I shapshotted it aboveAnotherMethodWhereWorkIsDone()
?Your assumptions are wrong. The compiler will turn this:
System.Diagnostics.Debugger.Log(0,"", "Current thread is: " + Threading.CurrentThread.GetHashCode());
into this:
int tempInt = Threading.CurrentThread.GetHashCode();
string tempStr = string.Concat("Current thread is: ", tempInt.ToString());
System.Diagnostics.Debugger.Log(0,"", tempStr);JoeRip wrote:
But now it occurs to me that I have no idea if my method is guaranteed to be executing on the same thread all the way through. Is there some chance that after AnotherMethodWhereWorkIsDone() executes, my handler will be running on a different thread than when I shapshotted it above AnotherMethodWhereWorkIsDone()? Reply·Email·View Thread·PermaLink·Bookmark
No, that is not possible with plain old methods. (It is possible with
yield return
iterator methods, but that's another story.) -
Your assumptions are wrong. The compiler will turn this:
System.Diagnostics.Debugger.Log(0,"", "Current thread is: " + Threading.CurrentThread.GetHashCode());
into this:
int tempInt = Threading.CurrentThread.GetHashCode();
string tempStr = string.Concat("Current thread is: ", tempInt.ToString());
System.Diagnostics.Debugger.Log(0,"", tempStr);JoeRip wrote:
But now it occurs to me that I have no idea if my method is guaranteed to be executing on the same thread all the way through. Is there some chance that after AnotherMethodWhereWorkIsDone() executes, my handler will be running on a different thread than when I shapshotted it above AnotherMethodWhereWorkIsDone()? Reply·Email·View Thread·PermaLink·Bookmark
No, that is not possible with plain old methods. (It is possible with
yield return
iterator methods, but that's another story.)Thanks, the debugger.log answer does make sense. For the other part: so my method is guaranteed to remain on the same thread from beginning to end of execution? Even if I called a method from inside it asynchronously (using delegate.BeginInvoke on its delegate)? That makes me happy. :-)