title: How to access a program's main loop in .NET (Winforms)
body: <p>This one is a little hard to explain, and I hope I explain it correctly.</p>
  
  <p>I am trying to access the main application's loop of a .NET program.  The reason why I must do this, is because I must access the loop of the main program's thread, so that I can <em>pause</em> the entire application with a breakpoint, or sleep function.</p>
  
  <p>I know that the code <strong><em>Application.Run(Form)</em></strong> is how to execute a form, but what can I do in order to get access to that form's main program loop?  I am trying to avoid using the Timer event handlers for this reason:  When I put <em>sleep</em> functions in the Timer event handlers, the program's animation continues to run in the background (is this because the Timer event handlers are a separate thread?).  But I need the program to come to a complete halt, like it is frozen in time.  The goal is to step edit through the loop, and have the program's animation execute frame by frame, only as I step edit through the main program's loop.</p>
  
  <p>I know this sounds strange, but this is the functionality I require.</p>
  
  <p>Thanks everyone!</p>
  
  <p>===============================================================</p>
  
  <p><strong>Edit:  A better explanation from a technical point of view.</strong></p>
  
  <p>===============================================================</p>
  
  <p>In C, the main window entry point is:</p>
  
  <pre><code>int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPreInst,LPSTR lpszCmdLine,int nCmdShow)
  </code></pre>
  
  <p>In this function, we have a message loop code, such as:</p>
  
  <pre><code>while(GetMessage(&amp;lpMsg,NULL,0,0))
      {   TranslateMessage(&amp;lpMsg);
          DispatchMessage(&amp;lpMsg);
      }
  </code></pre>
  
  <p>Within that function, another function called "CreateWindow" creates the main window, then jumps to a Callback function called "WndProc" which is the equivalent of event handlers in .NET.  The event handler for the timer function is:</p>
  
  <pre><code>LRESULT CALLBACK WndProc(HWND hWnd,UINT messg,WPARAM wParam,LPARAM lParam)
      case WM_TIMER:          
          CODE:
          return DefWindowProc(hWnd,messg,wParam,lParam);
  </code></pre>
  
  <p>My goal is to get access to the message loop, in this case, the while loop that executes the WM_TIMER case, of the main entry point in .NET, the same way you do in C.</p>
  
  <p>Here is a reference link to the program flow I described above:</p>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ff381409%28v=vs.85%29.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/windows/desktop/ff381409%28v=vs.85%29.aspx</a></p>