Threading question

A

arun.hallan

I've read up on threading but it's confusing me somewhat.

The functionality i need is as so:

I have my main method which calls two other methods in two different
classes.

Each of these methods involves waiting for database operation to occur,
so would be better firing these two methods off at the same time, i.e.
in two threads.

So i need these two methods to be prompted from one method, and when
each of them finish the normal process can carry on. Also these two
methods need to return a DataSet each.

Can anyone help?
 
N

Nicholas Paldino [.NET/C# MVP]

There are some things to keep track of here.

First, you should have the code that accesses the database in two
separate methods (representing the two operations you want to perform).

On top of that, they should use two separate connections, and I assume
the two operations won't step on each other.

When you create the threads, you can call the Join method on the thread
to wait for the threads to complete. However, this is NOT the recommended
way to do this.

What you should do is create two events, one for each method. In your
method, wrap your code in a try/finally block, and then set the events in
the finally block.

In your calling code, after you have kicked off your threads, wait for
the events (you can pass an array of them to the static WaitAll method on
the WaitHandle class, or, just call WaitOne on each event).

Then, you can return to your method.

Hope this helps.
 
A

arun.hallan

Both posts have helped, thanks a lot.
There are some things to keep track of here.

First, you should have the code that accesses the database in two
separate methods (representing the two operations you want to perform).

On top of that, they should use two separate connections, and I assume
the two operations won't step on each other.

When you create the threads, you can call the Join method on the thread
to wait for the threads to complete. However, this is NOT the recommended
way to do this.

What you should do is create two events, one for each method. In your
method, wrap your code in a try/finally block, and then set the events in
the finally block.

In your calling code, after you have kicked off your threads, wait for
the events (you can pass an array of them to the static WaitAll method on
the WaitHandle class, or, just call WaitOne on each event).

Then, you can return to your method.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I've read up on threading but it's confusing me somewhat.

The functionality i need is as so:

I have my main method which calls two other methods in two different
classes.

Each of these methods involves waiting for database operation to occur,
so would be better firing these two methods off at the same time, i.e.
in two threads.

So i need these two methods to be prompted from one method, and when
each of them finish the normal process can carry on. Also these two
methods need to return a DataSet each.

Can anyone help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top