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?
Have a look at the ThreadPool class. It should put you in the right place for easy handling of multi threaded applications
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.
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.
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.
}
this link will help you:
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
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.
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,
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