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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

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.  
i think you should read articles given below. they are simple, basic and straighfwd..
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

countrymeisterAuthor 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

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

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) {

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

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.


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?
countrymeisterAuthor 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.
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
countrymeisterAuthor Commented:
I will try to run your sample and see what I learn from it, thanks
countrymeisterAuthor Commented:
This is a great start for my multi threading learning.
Thanks a lot
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.