On Thu, Feb 5, 2009 at 7:00 AM, Nacho108 <[email protected]> wrote:
>
> Hi everyone!
>
> I'm trying to start using threads in this application I'm making and
> I'm having some problems.
> The task is simple: I want to read a database with 200000 records
> that's situated in another PC, so it takes some seconds. Then I want
> to show them in a datagridview control.
> The thing is that I'm trying to fill a dataset which I define in the
> main thread and I cannot access it within the "worker" thread, and If
> I define it within the worker thread I cannot send it to the main
> thread.
>
> Can somebody explain to me how people solve this problem?
>
> Thanks!!
> Nacho
As Steve pointed out, cross-thread stuff needs to be handled differently.
Do you just want a nice way to wait for the DB to return the records without
the interface locking up?
If so, you can use an async call for a resultset.
For MS SQL Server, you need to add ";Asynchronous Processing=true" to
your connection
string.
Then do something like:
int counter = 0;
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_FooBar";
cmd.Parameters.Add(); // add any params you need
cmd.CommandTimeout = 0;
IAsyncResult result = cmd.BeginExecuteNonQuery();
while (!result.IsCompleted) // wait till sproc is done
{
DoSomethingWhileWeWaitLikeSleepOrDoEventsEtc();
}
cmd.EndExecuteNonQuery(result);