PC Review


Reply
Thread Tools Rate Thread

Calling Fill on a Seperate Thread causes Application to crash

 
 
user@domain.invalid
Guest
Posts: n/a
 
      3rd Aug 2003
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

 
Reply With Quote
 
 
 
 
AlexS
Guest
Posts: n/a
 
      3rd Aug 2003
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
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Brian Takita
Guest
Posts: n/a
 
      5th Aug 2003
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

AlexS wrote:
> 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
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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
>>

>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Application crash when calling the Help.ShowHelp method nagar@community.nospam Microsoft C# .NET 0 18th Oct 2008 03:27 PM
Main thread calling event from child thread without using Ctrl.Inv =?Utf-8?B?Tm91bG91aw==?= Microsoft Dot NET Framework Forms 7 5th Jul 2006 01:58 AM
Calling a main thread method from a worker thread Hao L Microsoft C# .NET 5 2nd May 2005 08:31 PM
aborting thread results in application crash Snuyt Microsoft VB .NET 1 3rd Nov 2004 07:34 PM
SplashScreen : Calling a method in a seperate thread from main form mike c Microsoft Dot NET Framework Forms 1 7th Apr 2004 06:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:16 PM.