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?
-
you seem to be saying you want to use multithreading to run processes one after each other (i.e. sequentially)?Mitch Wheat– Mitch Wheat2010-03-18 07:22:26 +00:00Commented Mar 18, 2010 at 7:22
-
Mitch Wheat not on after one simultaneous. I posted again because i am not satisfied with previous postingRed Swan– Red Swan2010-03-18 09:35:45 +00:00Commented Mar 18, 2010 at 9:35
-
don't repost exactly the same question, it will be closed. If you're not happy with the answers, edit your question to make the intent clearer. If the editing would result in the question being markedly different, then, and only then, ask the question again.Rob– Rob2010-03-18 09:55:42 +00:00Commented Mar 18, 2010 at 9:55
-
Also - it looks like like English might not be your first language, but still, try breaking your question up into paragraphs. It'll help others make sense of the question and give you an appropriate answer.Rob– Rob2010-03-18 09:56:11 +00:00Commented Mar 18, 2010 at 9:56
-
right english is not my 1'st lng.Red Swan– Red Swan2010-03-18 10:04:30 +00:00Commented Mar 18, 2010 at 10:04
7 Answers
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.
}
6 Comments
Have a look at the ThreadPool class. It should put you in the right place for easy handling of multi threaded applications
Comments
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.
2 Comments
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.
5 Comments
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.
Comments
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,
Comments
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