yasmuru
yasmuru

Reputation: 1186

Datagridview with Multithread in c#

Is that possible to read data from DataGridView using Multi thread . Or is there any other way ? Can anyone give me an example or concept ?

I wants to read the data from datagridview row by row. And pass each row as parameters to an method .

Please help me in this ...

Upvotes: 2

Views: 1647

Answers (3)

Brian Gideon
Brian Gideon

Reputation: 48949

No, it is not safe to access a DataGridView (or any Control for that matter) on any thread other than the main UI thread. This includes reading data from the control. You just cannot do this safely.

What you need to do is maintain a separate data structure that holds the data and then read from that. It is extra work because now you have to keep the DataGridView in sync with the other data structure. If you are manually adding rows to the grid then it might be easy. If you are data binding the grid (via BindingList or whatever) then it might be a bit trickier.

Keep in mind that tricks involving Contol.Invoke will not achieve the concurrent read behavior since the actual read would have been marshaled onto the UI thread. You might as well avoid using multithreading altogether. However, what you might be able to do is read the data rows individually on the UI thread, make a copy of them into a POCO data structure, and then shuttle the POCO object to the method you want called and have it execute in a Task or exiting worker thread.

Upvotes: 0

Deadlock
Deadlock

Reputation: 330

// define a thread, deligate,list

       public List<string> temp_list= new List<string>();
       public Thread Run_thread = null, run1 = null;
       private delegate void UpdateDelegate(); 

//start the thread during the form load event... and then pause it until you want to start..

        run1 = new Thread(new ThreadStart(run_tab));
        run1.IsBackground = true;
        run1.Start();

        Datagridview dg = new Datagridview();
        dg.Rows.Add();

//this below function will be triggered when you press some button.....and it will start the thread to do its job..

   private void Btn_run_Click(object sender, EventArgs e)
    {
       if (run1.ThreadState == System.Threading.ThreadState.Aborted)
         {
            try
            {
                myResetEvent.Reset();
            }
            catch (Exception ex)
             {
                 throw new Exception("Exception message:" + ex.Message);
              }
          }
           else
            {
               try
                  {
                    myResetEvent.Set();
                   }
                   catch (Exception ex)
                    {
                     throw new Exception("Exception message:" + ex.Message);
                   }
           }
    }

// after adding several rows you want thread should pass each rows values from gridview to some method.

       private  void run_tab()
          {

              while (true)
              {
                 myResetEvent.WaitOne();

// this above command will stop the thread from execution till you press some button or some other event or means to say when you want thread to start doing its operation

// as per my appraoch read once complete gridview rows and store all their values in temp list once it is done then use those values to run background thread..

            if (InvokeRequired)
                     {
                 Invoke(new UpdateDelegate(
                  delegate
                  {
                       int rowcount = dg.Rows.Count;

                    for (b = 0; b < rowcount; b++)
                      {
                         string temp= dg.Rows[b].Cells[0].Value.ToString();
                         temp_List.Add(type1);
                      }
                   })
                   );
                 } 

// now done with temporary storage.... // now do normal thread working.....

          for (int i = 0; i < temp_List.Count; i++)
                  {
                // some moethod xyz where u want o pass those values
                   method_XYZ(temp_List[i]);
                  }


     }
     }

hope this will give some solution to your problem...

Upvotes: 0

Binu Bhasuran
Binu Bhasuran

Reputation: 468

Do you want to read data isn't?, why don't you just read it from underlying data source? possibly a data table.

if you can get me more clarity on the scenario which you are trying, will help to provide better solution. Since data table is bound to grid, you need to access it via UI thread.

Here is sample code using Task.

 public Task<DataRow> ReadData(DataTable table, int rowId)
   {
       return Task<DataRow>.Factory.StartNew(() => table.Rows[rowId]);
   }

you need sync mechanism to access this row, since its runs in different thread,

Upvotes: 1

Related Questions