title: How to force a piece of code inside a thread to run uninterruptedly (keep it from being preempted by the OS)
body: <p>For a given task T, and blocks of code (wrapped in methods here) m1, m2 and m3, </p>
  
  <p>is there a way to force any one of them - say m2 - to run uninterruptedly, i.e., that the thread running this program, upon reaching its time slice limit, if in the middle of execution of m2, do not leave the processor until m2 finishes, only then making room for a new thread ?</p>
  
  <p>Is this possible ?</p>
  
  <p>Example:</p>
  
  <pre><code>class Program
      {
          static void Main(string[] args)
          {
              Task task = new Task(() =&gt;
              {
                  m1();
  
                  //I want to assure m2 runs uninterruptedly, i.e., that the running thread does not stop while executing m2
                  m2();
  
                  m3();
              });
          }
  
          private static void m1()
          {
              //whatever1...
          }
  
          private static void m2()
          {
              //whatever2...
          }
  
          private static void m3()
          {
              //whatever3...
          }
      }
  </code></pre>
  
  <p>Answering everybody's "why did you come up with this?", it popped up while trying to find a solution to <a href="https://stackoverflow.com/questions/31654380/multi-threaded-c-sharp-selenium-webdriver-automation-with-uris-not-known-beforeh"><strong>this</strong></a> problem. What I thought: I need to guarantee that my switches from and to the main window handle is not interrupted/preempted. </p>
  
  <p>Whether this is a plausible solution for my real problem or not - I thought the question was worth asking.</p>