title: Multithreading with Datagridview C#
body: <p>Here i'm doing one process of checking user names. I've created one datagridview and loadded data from the text file. so the datagridview contains first name and last name of users in first two columns . What i need to do is read those values row by row and find that there is no same name for first and last .These operation is performed in separate class. So i need to get result from that class and show the result to gridview alternatively . It all works fine when i'm using just one thread . But when i'm using more than one thread it just throwing an exception that outofbound exception in gridview reading . Here is my coding :</p>
  
  <pre><code>static int i, j=0, l=0,k=0,m=0;
  
  public void check()
  { 
      for (i = 0; i &lt; dataGridView1.Rows.Count ; i++)
      {
          if (InvokeRequired)
          {
              Invoke(new UpdateDelegate(delegate
                  {
                      if (i == 0 &amp;&amp; j==0)
                      {
                          DataGridViewColumn col = new DataGridViewTextBoxColumn();
                          col.HeaderText = "Status";
                          int colIndex = dataGridView1.Columns.Add(col);
                          dataGridView1.Rows[i].Cells[colIndex].Value = "Process Started";
                          j = 1;
                      }
                      else
                      {
                          dataGridView1.Rows[i].Cells[3].Value = "process Started";            
                      }
                  }));
          }
  
          if (InvokeRequired)
          {
              Invoke(new UpdateDelegate(delegate 
                  { 
                      Process obj_reg = new Process(dataGridView1.Rows[i].Cells[1].Value.ToString(),dataGridView1.Rows[i].Cells[2].Value.ToString());
                      string res = obj_reg.register();
                      Thread.Sleep(500);
                      if (res.Contains("success"))
                      {
                          if (i == 0 &amp;&amp; k==0)
                          {
                              DataGridViewColumn col = new DataGridViewTextBoxColumn();
                              col.HeaderText = "Result";
                              int colIndex = dataGridView1.Columns.Add(col);
                              dataGridView1.Rows[i].Cells[colIndex].Value = "Ya its different";
                              dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
                              k = 1;
                          }
                          else 
                          {
                              dataGridView1.Rows[i].Cells[4].Value = "Ya its different";                              
                              dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
                          }
                      }
                      else
                      {
                          if (i == 0 &amp;&amp; m == 0)
                          {
                              DataGridViewColumn col = new DataGridViewTextBoxColumn();
                              col.HeaderText = "Result";
                              int colIndex = dataGridView1.Columns.Add(col);
                              dataGridView1.Rows[i].Cells[colIndex].Value = "No its same";
                              dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                              m = 1;
                          }
                          else
                          {
                              dataGridView1.Rows[i].Cells[4].Value = "No its same";
                              dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
  
                          }
                      }
                  }));
          }
      }
  }
  
  public void Button1_Click(Object sender, EventArgs e)
  {
      Thread[] threads = new Thread[3];
      for (int l = 0; l &lt; 3; l++)
      {
          threads[l] = new Thread(new ThreadStart(check));
      }
      foreach (Thread t in threads)
      {
          t.Start();
      }
  }
  </code></pre>
  
  <p>Please suggest me how to use Multithread here ... its just an example .. please explain any other way ..</p>