Link to home
Start Free TrialLog in
Avatar of countrymeister
countrymeister

asked on

Console Application with Multi Threading

I am fairly new to Threading. I currently need to build a console application which will take in some input parameters and return a code
Within the console application I need to create two threads.
Each thread updates database records within SQL Server 2005.
At the begin of each thread process I need to write to a file the Start Time and other information such as Records processed and the End time when the thread completes/ends.
The file to be written to is the same file for both threads.

any code samples will be great.
Avatar of josgood
josgood
Flag of United States of America image

Perhaps this will get you started.

The important points are:
1)  Synchronizing the two threads so that only one at a time accesses a resource.  Hence the lock().
2)  Waiting for threads to complete.

You'll have to make the database connection, of course.  I just left a null SqlConnection to illustrate.

Joe
Program.txt
I just noticed that
         if (!thread2.Join(waitForJoin)) thread1.Abort();
should be
         if (!thread2.Join(waitForJoin)) thread2.Abort();
(terminate the same thread you're checking)

And, of course, the commented out code
         //lock(conn) {  // Only one thread at a time
         //   // Write a record to the database
         //}
needs to be replaced with something real.  
Avatar of xPert_Umer
xPert_Umer

well..
i think you should read articles given below. they are simple, basic and straighfwd..

http://www.programmersheaven.com/articles/arun/multithread.htm
http://www.yoda.arachsys.com/csharp/threads/
Avatar of countrymeister

ASKER

Hi ! JosGood

The code sample you provided, I have  a few qts

Will these line not cause any problems
Since the code is excuted top bottom

 // Don't exit until both threads complete
         if (!thread1.Join(waitForJoin)) thread1.Abort();
         if (!thread2.Join(waitForJoin)) thread2.Abort();
         
         // flush the file and close it
         sw.Flush();
         sw.Close();
         fs.Close();        

Because say the if condition is reached and the threads are  still running , this will result in the if conditins failing and control will flow to the StreamWriter flush and if there is no lock on the sw. Which could happen when the thread is doing the database portion of processing, this could result in a hung application, because you would get an exception when you try to write to the Stream write which is already closed.
Take a look at
http://msdn2.microsoft.com/en-us/library/system.threading.thread.join.aspx

As discussed there, the threadx.Join will block the main thread for a period of time -- waitForJoin ticks in the example code.  Given that the database portion of processing may take a difficult-to-determine amount of time, you may want to do something like this
    while (!thread1.Join(aTimePeriod) {
        if (the thread is not processing normally) {
           thread1.abort();
           break;
        }
    }

This of course, presupposes that you have a way to tell that the database processing is proceeding normally.  If you are doing several Selects or Updates or whatnot, you might be able to say that each one will take no longer than X time, and so perhaps the Join time period might be 2*X -- depending on what other processing you have going on.

As you say, you don't want to sw.Close() while the threads are still running.
JosGood,

Thanks for the explanation. So if the join statement will cause the main thread to wait for the worker thread1 and thread2, I could do this.

thread1.Join();
thread2.Join();

I do not want to abort the thread after time period of X, since I have no idea how long the thread processing could take. Is there any way I can notify the main thread that the worker thread has finished processing.
You certainly can do as you say.

If your threads each use a different thread procedure, then you could define a boolean for each (at the class level) and have the thread procedure set that boolean when complete.

If your threads each use the same thread procedure, then something more elegant is needed.  I know that the Thread contains a managedThreadID and a name.  I'll look into how to use these -- assuming you need the more elegant solution.

Do you?
I would prefer the more elegant solution. I have been reading into AutoResetEvent and ManualResetEvents. Not clear how these can help in notifying the calling thread that the worker thread has finished.
ASKER CERTIFIED SOLUTION
Avatar of josgood
josgood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I will try to run your sample and see what I learn from it, thanks
This is a great start for my multi threading learning.
Thanks a lot