title: Can a context switch occur during a method call?
body: <p>I am working on an application that interacts with a database via stored procedures. To prevent the GUI being unresponsive i run the stored procedures each in a different thread. I tested this with a loop in a thread and that works, but when i call a stored procedure which has to return very much records, the GUI is still blocking. </p>
  
  <p>Now my question is: can a context switch occur when a method call (the stored procedure in this case) is not finished yet? I know that c# code is compiled to IL. Is it possible that an IL-instruction is getting all the Threading resources instead of the GUI thread ? When occurs a context switch to another process and is it different from context switching to different threads belonging to the same process ?</p>
  
  <p>Edit:</p>
  
  <p>This is how i start the thread:</p>
  
  <pre><code>private void button2_Click(object sender, EventArgs e)
      {
          filldb = new Thread(adddummytodb);
  
          filldb.Start();
      }
  </code></pre>
  
  <p>This is the code in the thread with the stored procedure call. When i run this, the GUI hangs directly however i know that the stored procedure call lasts a noticable time because it gets +100000 records. When does the thread end ? When the invoked filldgv method is called or when it is finished ?</p>
  
  <pre><code>    private void adddummytodb()
      {
          Eigenaars = dc.sp_SearchEigenaar("", "", "", null, null);
  
          this.dataGridView1.Invoke((controlInvoke)filldgv);
  </code></pre>
  
  <p>When i Run this code with a loop of short duration stored procedure calls, the GUI stays responsive:</p>
  
  <pre><code>private void adddummytodb()
      {
          int ts;
  
          for (int i = 0; i &lt; 2000; i++)
          {
              if (stopthread == false)
              {
                  ts = dc.sp_SaveCst(0);
              }
              else
                  break;
          }
  </code></pre>