473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using the BackgroundWorker and a SqlDataReader

Hi,

I've written a simple app which should just fetch some data from a
database and render the results into a ListView.

In order to not freeze the GUI, I'm using a BackgroundWorker. The
arguments for the RunWorkerAsync are the Query to be executed and the
Connectionstring. As a result I hoped I could return a SqlDataReader.

The problem is, if I create the SqlDataReader within the
BackgroundWorker Thread, I can't access it after the Thread has exitted.
If I would create it on the Form (thus within the Forms Thread) and pass
it as an argument to the Thread, I might end up with the risk of two
Threads accessing the same resource (a Forms Property in this case) and
that doesn't sound a nice thing to do.

How can I pass the resulting SqlDataReader from the Thread back to the form?

Any help is greatly appreceated and thanks in advance!

Matthias
Nov 16 '05 #1
2 7346
Matthias,

Can you show an example of how you are using the BackgroundWorker class?
AFAIK, you shouldn't have an issue if you create a SqlDataReader and pass it
around (the OleDb provider I can understand, but not the SqlProvider).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matthias S." <po*****@emvoidSPAMTRAP.de> wrote in message
news:uB**************@TK2MSFTNGP09.phx.gbl...
Hi,

I've written a simple app which should just fetch some data from a
database and render the results into a ListView.

In order to not freeze the GUI, I'm using a BackgroundWorker. The
arguments for the RunWorkerAsync are the Query to be executed and the
Connectionstring. As a result I hoped I could return a SqlDataReader.

The problem is, if I create the SqlDataReader within the BackgroundWorker
Thread, I can't access it after the Thread has exitted. If I would create
it on the Form (thus within the Forms Thread) and pass it as an argument
to the Thread, I might end up with the risk of two Threads accessing the
same resource (a Forms Property in this case) and that doesn't sound a
nice thing to do.

How can I pass the resulting SqlDataReader from the Thread back to the
form?

Any help is greatly appreceated and thanks in advance!

Matthias

Nov 16 '05 #2
Hi Nicholas,

I'm trying to copy/paste the most relevant parts. All ErrorHandling has
been removed. Basically, we need to know only the two EventSinks for the
following BackgroundWorker events:

- DoWork
- RunWorkerCompleted
This is the EventSink for the DoWork event:

private void OnWorkerStart(object sender, DoWorkEventArgs args) {

// the DBWorkerArgs contain two public fields which are
// quite selfexplanatory: ConnectionString and Query
DBWorkerArgs workerArgs = (DBWorkerArgs) args.Argument;

// open connection
SqlConnection con = new SqlConnection(workerArgs.ConnectionString);

con.Open();

// run the query again, checks on the SqlConnection ommitted...
SqlCommand cmd = new SqlCommand(workerArgs.Query, con);
SqlDataReader reader = cmd.ExecuteReader();

// to make the Results available in the
// BackgroundWorker.RunWorkerCompleted event, we put them
// into the args.Result

args.Result = reader;

con.Close();

// done...
}

This is the EventSink for the RunWorkerCompleted event:

private void OnWorkerCompleted(object sender,
RunWorkerCompletedEventArgs args) {

// typechecking omitted
SqlDataReader reader = (SqlDataReader) args.Result;

// _ResultList is a ListView Control on the Form
_ResultList.Items.Clear();
_ResultList.Columns.Clear();

// columns first
int nFieldCt = reader.FieldCount;

for (int i = 0; i < nFieldCt; i++) {
_ResultList.Columns.Add(reader.GetName(i), 100);
}
}

Both methods are located withing Form1.

Thanks already for looking into my issue :)

Matthias

Nicholas Paldino [.NET/C# MVP] wrote:
Matthias,

Can you show an example of how you are using the BackgroundWorker class?
AFAIK, you shouldn't have an issue if you create a SqlDataReader and pass it
around (the OleDb provider I can understand, but not the SqlProvider).

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the...
3
by: Pro1712 | last post by:
Hi, this may be a stupid question: How can I can call the DoWork-function of a BackgroundWorker synchronous? Or in other words: How can I extend the BackgroundWorker class with a function...
11
by: GVN | last post by:
Hi All, Can anyone guide me when asynchronous method calls will be benificial? Are there any disadvantages of using asynchronous calls? Thanks,
1
by: Bob | last post by:
Hi, I am having trouble seeing how this bolts together. The UI starts a process which involves a long running database update. All Database activity is handled by a class called DT. DT has a...
5
by: redear | last post by:
Is there a way to immediately terminate a BackgroundWorker thread? My problem is that the BackgroundWorker starts with a call to My.Computer.FileSystem.GetFiles that can run for a very long time if...
5
by: Michael M. | last post by:
I have the following code (listed at bottom of post) that pings a small range of IP address to see which ones are alive. To speed things up a little I am trying to use more than one thread,...
0
by: Chris | last post by:
I would like to be able to pass the BackgroundWorker object and DoWork Event Args to a second function (third function?) and be able to still report the progress. I'm getting the following...
2
by: Peter Carlson | last post by:
I am testing backgroundWorker to update our installer with both messages and with a progress bar. I am finding however that the text never gets updated. Any ideas what I might be doing wrong? ...
0
by: PeterSchwennesen | last post by:
Problems starting a Timer Programmatically within a BackgroundWorker. I am trying to start a Timer inside a Backgroundworker. I want to start the BackGroundWorker and then have a timer tick a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.