1

I have around 10 threads updating tables in a datatable, each running every millisecond.

When running more than 1 I get the error Index is outside the bounds of the array.

Heres what I have tried so far

public DataTable ThreadsTable = new DataTable();

ThreadsTable.Columns.Add("Thread", typeof(string));
ThreadsTable.Columns.Add("Last", typeof(string));
ThreadsTable.Columns.Add("TMS", typeof(int));

Form1.form.ThreadsTable.Rows.Add("GetTickers", "0", 0);
Form1.form.ThreadsTable.Rows.Add("KeepAlive", "0", 0);
Form1.form.ThreadsTable.Rows.Add("WeekDay", "0", 0);
Form1.form.ThreadsTable.Rows.Add("DailyProfit", "0", 0);
Form1.form.ThreadsTable.Rows.Add("NewOrderCheck", "0", 0);
Form1.form.ThreadsTable.Rows.Add("NewOrderLocate", "0", 0);
Form1.form.ThreadsTable.Rows.Add("NewOrderProc", "0", 0);
Form1.form.ThreadsTable.Rows.Add("CloseOrderCheck", "0", 0);
Form1.form.ThreadsTable.Rows.Add("CloseOrderProc", "0", 0);

tried these to update, both give error

Form1.form.ThreadsTable.Rows[8]["Last"] = startdt.ToString("mm:ss:FFF");
Form1.form.ThreadsTable.Rows[8]["TMS"] = mscomplete;

and

foreach (DataRow row in Form1.form.ThreadsTable.Rows)
{
    if (row["Thread"].ToString() == "NewOrderCheck")
    {
        row["Last"] = startdt.ToString("mm:ss:FFF");
        row["TMS"] = mscomplete;
    }
}

Is there anyway to archive this using datatables? The reason I am doing it is I was directly updating a datagridview from all the threads which was causing other datagridviews to hang and need time to catch up. This way I can loop through the datatable each second and only update the main datagridview then.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
adam moore
  • 47
  • 5
  • [Thread safety for DataTable](https://stackoverflow.com/a/21328343/719186) – LarsTech Jul 24 '20 at 20:42
  • @LarsTech Should I just look at another method to do this? is there another alternative to a datatable which would allow concurrent updating to this extent? – adam moore Jul 24 '20 at 20:48
  • 1
    The user can't keep track of data changes every millisecond. So why do it so often? – Alexander Petrov Jul 24 '20 at 22:00
  • You can try using pipeline. Many writer-threads add values to a thread-safe collection, such as BlockingCollection. A single thread-reader retrieves this data and changes the DataTable. – Alexander Petrov Jul 24 '20 at 22:03
  • @AlexanderPetrov So, make it every [13 milliseconds](http://news.mit.edu/2014/in-the-blink-of-an-eye-0116) then? ;) Seriously though, I agree with you. I don't think displayed data needs to be updated that often. – 41686d6564 stands w. Palestine Jul 24 '20 at 22:05
  • I ended up using a dictionary with a KeyPairValue. The threads cant be slowed down due the purpose of the threads originaly. Yes I dont need to know every milliseconds value from the threads but I still needed to update the datagridview with values to show the speed the threads are executing in(without slowing the threads down just to update a datagridview). – adam moore Jul 24 '20 at 22:39

2 Answers2

0

Updates on the DataTable should be done on the main thread. That way, the data bound GUI elements will be updated on the GUI thread too as it should be. Refer to this question on how to achieve this: Calling methods in main thread from other threads

Tarik
  • 10,810
  • 2
  • 26
  • 40
0

DataTables are not thread safe. Check the MSDN documentation (All the way at the bottom). It is safe to read from a DataTable via multiple threads but not to write. You can serialize access using locking:

        lock (datatable)
        {
           //Do your updates
        }
JoshBerke
  • 66,142
  • 25
  • 126
  • 164