views:

137

answers:

8

Hi, I have console application. In that i have some process that fetch the data from database through different layers ( business and Data access). stores the fetched data in respective objects. Like if data is fetched for student then this data will store (assigned ) to Student object. same for school. and them a delegate call the certain method that generates outputs as per requirement. This process will execute many times say 10 times. Ok? I want to run simultaneously this process. not one will start, it will finish and then second will start. I want after starting 1'st process, just 2'nd , 3rd....10'th must be start. Means it should be multithreading. how can i achieve this ? is that will give me error while connection with data base open and close ? I have tried this concept . but when thread 1'st is starting then data will fetched for thread 1 will stored in its respective (student , school) objects. ok? when simultaneous 2'nd thread starts , but the data is changing of 1'st object ,while control flowing in program. What have to do?

A: 

Have a look at the ThreadPool class. It should put you in the right place for easy handling of multi threaded applications

PieterG
A: 

Yes this would require multithreading but I feel it makes a poor choice for a first attempt. Multithreading is complicated and requires you to "unlearn" a few things you picked up from procedural programming. This is further complicated by simultaneous database connections to the same database engine which may or may not improve your performance anyway.

Spencer Ruport
So what should the another solution. This is must that all process should execute at a times. This is time (in fact milliseconds dependency requirement in application)base application.
Lalit
It's not possible to do that. I'd suggest re-evaluating the problem and considering an alternate solution. While there are specialized operating systems out there that give you this kind of control over execution Windows does not. You may be able to come up with a solution that works 90% but you will have no guarantees.
Spencer Ruport
A: 

Have a look at Parallel Programming in .NET 4.0. Especially the parallel task library gives you great control over threading different tasks that can have dependencies on each other.

Eric J.
my application is in .Net 3.5 . is this provided in 3.5 framework?
Lalit
No, it's new to 4.0. But if you are doing threading seriously consider whether you can migrate to 4.0. It is MUCH easier and faster.
Eric J.
If you look at the comments he made on my answer he's hoping for millisecond precision which still won't be possible.
Spencer Ruport
@Spencer people confuse me sometimes! LOL :)
Lirik
@Spencer: True, Windows is not a real time operating system.
Eric J.
+1  A: 

Here is some sample code:

static void Main(string[] args)
{
    for (int i=0; i<10; i++)
        System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(DbWork));
}

public void DbWork(object state)
{
    // Call your database code here.
}
umbyersw
you don't think so it is dependent on for loop ? after one loop will executes then nd then second will start. I want all 10 process start at once . what you think ?
Lalit
@Lalit once the work is queued in the ThreadPool it will start working on a separate thread and the for loop will continue to spawn new threads while the item is being executed. It's equivalent to driving your car and throwing pebbles on the street: you throw the first pebble and it starts rolling, you throw the second pebble and it will most likely start rolling before the first pebble stops. If you REALLY insist on all the threads to start at the same time, then just let them all block on a ManualResetEvent and when the for loop completes just signal on the event. Have you heard of fork()?
Lirik
are you sure this will not make impact on sql database connection to open in each process and will close in same manner ? If sure then i am going to use it .. plz confirm.
Lalit
@Lalit there is no problem with having multiple simultaneous connections to the database, but make sure that the number of connections are reasonable. If you code your application to close the connection, then it will close them :)... but it won't close the connections simply because you've spawned multiple threads.
Lirik
.NET will make use of connection pooling, which you can manage using parameters in your connection string. The database connections will be closed or kept open depending on that connection pool.
umbyersw
Yes, you must of course call myConnection.Close(), or even better, use a using statement around your connection, and when each thread closes that connection or exits that using statement, the connection is returned to the pool.
umbyersw
+1  A: 

this link will help you:

DotNet Academy

you can modify this solution with threads and you will get your desired result. http://dotnetacademy.blogspot.com/2010/02/multiple-active-result-sets-mars-in-sql.html

Rajesh Rolen- DotNet Developer
A: 

You need to instantiate a new instance of each class for each thread. If each call to the database is modifying the data from the first call, you are referencing the same instance. Multithreading is considered an "advanced topic", you may want to find another way to solve this problem if at all possible.

DFX
A: 

this may help!

start a thread that is master thread that calls all threads 1,2,3 .... in sequence and do this logic to master thread

master thread started..

thread 1 started if first time or resumed if not first time work completed suspend t1

start t2 is first time or resume t2 if not first time

t2 work complete t2 suspend

t3 started or resumed as condition handled first

and so on for all thread

the master thread will control the sequence for all thread and suspend and resume them also

try this,

moon
A: 

You should not implement a multithreaded solution unless you understand the mechanisms and inherent problems.

That said, when you feel you are ready to move to a parallel algorithm, use the pattern known as APM (Asynchronous Programming Model) where you spawn the worker threads and let them notify the main thread via callback methods (i.e. event delegates).

Jeffrey Richter explains: Implementing the CLR Asynchronous Programming Model

d91-jal