We help IT Professionals succeed at work.

Console Application with Multi Threading

countrymeister
on
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.
Comment
Watch Question

Top Expert 2007

Commented:
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
Top Expert 2007

Commented:
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.  
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/

Author

Commented:
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.
Top Expert 2007

Commented:
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.

Author

Commented:
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.
Top Expert 2007

Commented:
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?

Author

Commented:
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.
Top Expert 2007
Commented:
That was fun!

I decided to keep a Dictionary, tracking the state of each thread.  A thread can be running, finished, or joined.  The main thread creates a worker thread and puts it in the Dictionary with status running.  Once the worker thread finishes, it updates the Dictionary to show that. The main thread then discovers that the Dictionary has changed, and joins the thread.

Take a look at the attached file and see if it makes sense to you.

Joe
Program.txt

Author

Commented:
I will try to run your sample and see what I learn from it, thanks

Author

Commented:
This is a great start for my multi threading learning.
Thanks a lot

Explore More ContentExplore courses, solutions, and other research materials related to this topic.