Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Does a method execute on the same thread for its entire length?

Does a method execute on the same thread for its entire length?

Scheduled Pinned Locked Moved C#
debuggingquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    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 above AnotherMethodWhereWorkIsDone()?

    J 1 Reply Last reply
    0
    • J JoeRip

      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 above AnotherMethodWhereWorkIsDone()?

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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.)

      J 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        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.)

        J Offline
        J Offline
        JoeRip
        wrote on last edited by
        #3

        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. :-)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups