S
Shaeney
I have a multi-threaded application with a static constructor and static
DataSet and DataView variables, populated in the constructor.
i then create many threads to access the static DataView but some of the
threads generate errors while others work. Here is the code for the sample
console app
using System;
using System.Data;
using System.Threading;
namespace DataViewTest
{
class DataViewTest
{
static DataSet _ds;
static DataView _dv;
private volatile Thread _thread;
private string _name;
static DataViewTest()
{
_ds = new DataSet();
DataTable tableOne = new DataTable("TableOne");
tableOne.Columns.Add(new DataColumn("Force", typeof (int)));
tableOne.Columns.Add(new DataColumn("Section", typeof (string)));
tableOne.Columns.Add(new DataColumn("ApplicationID", typeof (string)));
tableOne.Columns.Add(new DataColumn("GroupID", typeof (int)));
tableOne.Columns.Add(new DataColumn("Name", typeof (string)));
DataRow newRow = tableOne.NewRow();
newRow["Force"] = 13;
newRow["Section"] = "0";
newRow["ApplicationID"] = "FA";
newRow["GroupID"] = 1;
newRow["Name"] = "ZZ";
tableOne.Rows.Add(newRow);
_dv = new DataView(tableOne, null, "GroupID", DataViewRowState.CurrentRows);
_ds.Tables.Add(tableOne);
_ds.AcceptChanges();
}
public DataViewTest(string threadName)
{
_name = threadName;
}
internal void Run()
{
if(_dv[0]==null)
{
System.Diagnostics.Trace.WriteLine(string.Format("{0} FAILED", _name));
}
else
System.Diagnostics.Trace.WriteLine(string.Format("{0} worked", _name));
}
internal void Start()
{
_thread = new Thread(new ThreadStart(Run));
_thread.Name = _name;
_thread.IsBackground = true;
_thread.Start();
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private static DataViewTest[] _workers;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
_workers = new DataViewTest[100];
for (int ct=0; ct<_workers.Length; ct++)
{
_workers[ct] = new DataViewTest(string.Format("Thread '{0}'", ct));
}
for (int ct=0; ct<_workers.Length; ct++)
{
_workers[ct].Start();
}
while(true)
Thread.Sleep(1000);
}
}
}
here is some sample output
Thread '0' worked
The thread 'Thread '0'' (0x1dd4) has exited with code 0 (0x0).
Thread '27' worked
The thread 'Thread '27'' (0x1f18) has exited with code 0 (0x0).
Thread '63' worked
Thread '6' worked
Thread '1' FAILED
The thread 'Thread '6'' (0x1e70) has exited with code 0 (0x0).
The thread 'Thread '1'' (0x1b08) has exited with code 0 (0x0).
Thread '3' FAILED
The thread 'Thread '3'' (0x14d8) has exited with code 0 (0x0).
Thread '11' worked
Thread '2' FAILED
The thread 'Thread '11'' (0x1d90) has exited with code 0 (0x0).
The thread 'Thread '2'' (0x1bd8) has exited with code 0 (0x0).
Thread '5' worked
The documentation says that DataViews are threadsafe, so why is this
happening? Sometimes, everything is OK, but more often than not, at least one
thread fails.
DataSet and DataView variables, populated in the constructor.
i then create many threads to access the static DataView but some of the
threads generate errors while others work. Here is the code for the sample
console app
using System;
using System.Data;
using System.Threading;
namespace DataViewTest
{
class DataViewTest
{
static DataSet _ds;
static DataView _dv;
private volatile Thread _thread;
private string _name;
static DataViewTest()
{
_ds = new DataSet();
DataTable tableOne = new DataTable("TableOne");
tableOne.Columns.Add(new DataColumn("Force", typeof (int)));
tableOne.Columns.Add(new DataColumn("Section", typeof (string)));
tableOne.Columns.Add(new DataColumn("ApplicationID", typeof (string)));
tableOne.Columns.Add(new DataColumn("GroupID", typeof (int)));
tableOne.Columns.Add(new DataColumn("Name", typeof (string)));
DataRow newRow = tableOne.NewRow();
newRow["Force"] = 13;
newRow["Section"] = "0";
newRow["ApplicationID"] = "FA";
newRow["GroupID"] = 1;
newRow["Name"] = "ZZ";
tableOne.Rows.Add(newRow);
_dv = new DataView(tableOne, null, "GroupID", DataViewRowState.CurrentRows);
_ds.Tables.Add(tableOne);
_ds.AcceptChanges();
}
public DataViewTest(string threadName)
{
_name = threadName;
}
internal void Run()
{
if(_dv[0]==null)
{
System.Diagnostics.Trace.WriteLine(string.Format("{0} FAILED", _name));
}
else
System.Diagnostics.Trace.WriteLine(string.Format("{0} worked", _name));
}
internal void Start()
{
_thread = new Thread(new ThreadStart(Run));
_thread.Name = _name;
_thread.IsBackground = true;
_thread.Start();
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private static DataViewTest[] _workers;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
_workers = new DataViewTest[100];
for (int ct=0; ct<_workers.Length; ct++)
{
_workers[ct] = new DataViewTest(string.Format("Thread '{0}'", ct));
}
for (int ct=0; ct<_workers.Length; ct++)
{
_workers[ct].Start();
}
while(true)
Thread.Sleep(1000);
}
}
}
here is some sample output
Thread '0' worked
The thread 'Thread '0'' (0x1dd4) has exited with code 0 (0x0).
Thread '27' worked
The thread 'Thread '27'' (0x1f18) has exited with code 0 (0x0).
Thread '63' worked
Thread '6' worked
Thread '1' FAILED
The thread 'Thread '6'' (0x1e70) has exited with code 0 (0x0).
The thread 'Thread '1'' (0x1b08) has exited with code 0 (0x0).
Thread '3' FAILED
The thread 'Thread '3'' (0x14d8) has exited with code 0 (0x0).
Thread '11' worked
Thread '2' FAILED
The thread 'Thread '11'' (0x1d90) has exited with code 0 (0x0).
The thread 'Thread '2'' (0x1bd8) has exited with code 0 (0x0).
Thread '5' worked
The documentation says that DataViews are threadsafe, so why is this
happening? Sometimes, everything is OK, but more often than not, at least one
thread fails.