Calling Fill on a Seperate Thread causes Application to crash

  • Thread starter Thread starter user
  • Start date Start date
U

user

Hello,

I'm trying to call a Fill method from a DataAdapter object on a seperate
thread.

The Application crashes without seeming to throw any exceptions. Both of
these methods are in the same class.

Here is my source code:

public void Fill()
{
try
{
string commandText = da.SelectCommand.CommandText;
string where = "";

lock(da)
{
if (RowFilter != "")
{
where = " Where " + RowFilter;
da.SelectCommand.CommandText += where;
}
currentThread = new Thread(new ThreadStart(FillDataSet));
currentThread.Name = _table.TableName + " Fill";
currentThread.Priority = ThreadPriority.Normal;
currentThread.IsBackground = true;
currentThread.Start();

if (where != "")
{
da.SelectCommand.CommandText = commandText;
}
}
}
catch(Exception ex)
{
throw ex;
}
}

private void FillDataSet()
{
try
{
lock(_table)
{
MessageBox.Show(this.ToString());
da.Fill(_table);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
throw ex;
}
}


Thank You,
Brian Takita
 
Create data adapter in the filling thread - and connection too. It might
help. Pass back filled dataset only.

Note what docs are saying about components like DataAdapter

Thread Safety
Any public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Any instance members are not guaranteed to be
thread safe.


HTH
Alex
 
I found out what the problem was.

I was using a DataGrid control in my form. If I updated the datasource
on a seperate thread, the application crashed because Windows Forms
controls are not thread safe.

As a solution, I moved the threads out of the data access classes, and
used the form to control the threads.

I tried to disconnect the datasource from the DataGrid before the Fill
thread starts and reconnect the DataSource (through BeginInvoke) after
the thread finishes, but this caused the DataGrid to flicker.
The application also froze when I changed the DataSource a few times.

I will try to create a DataTable in the seperate thread and then replace
the current DataTable with the new one.

Brian
 
Back
Top