How to wait for a thread to finish before continuing? [CS/C# 2005]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
Here is the problem - I have a thread that has a dataset [dsList], this thread then launches another thread that uses this dataset [dsList] in its task and then original thread continues on to clear the dataset [dsList] itself...
Problem is, it looks like the Dataset is Cleared before the spawned thread gets to use the value (all timing), this does not produce the desirable effects...

So, how can I make the main thread WAIT for the spawned thread to finish its work BEFORE it clears the dataset [dsList]?
Any ideas, hints, and help would be greatly appreciated, thanks

BTW - don't mind contention on dsList, I have already taken care of that just not shown above
 
I have to ask... why not just clear the dataset in the thread that uses it? This would save the hassle since you know that you only want the dataset cleared AFTER it is used.

The point of threads is that they run simultaneously. Having the one thread pause while waiting on the second thread to finish defeats the purpose of threads to begin with.
 
Because the WorkerThread is used in a LOT of different areas where I do not always want to clear the dataset - only in this one instance do I want to do it ... I could pass in a BOOL and have an IF (true) in the THREAD for that ONE case but I thought that was ugly...

Most of the time they are simultaneously, but this ONE case is special (because of the CLEAR).
 
Not ugly... in this case it would be practical. It avoids having to pause your main thread thereby negating the purpose of running that routine at that time in a separate thread. Otherwise, just call the routine in the main thread. Either way the result is the same... the main thread will have to wait for the action to complete before it will do anything else.
 
mooman_fl said:
The point of threads is that they run simultaneously. Having the one thread pause while waiting on the second thread to finish defeats the purpose of threads to begin with.
I would disagree with this. A lot of times you want to overlap the execution of two threads in some way. With disk and network I/O you can leverage the asynchronous mechanisms .NET provides but you often need to communicate between two threads while preserving some sort of mutual exclusion. .NET provides many tools to do this.

To elaborate on what marble eater said -- If you just have a process that is running in another thread and you want the main thread to wait for that process to complete, then just use the Join method:
C#:
using System;
using System.Threading;

namespace ThreadingSynch
{
    class Program
    {
        static void SeparateThread()
        {
            Thread.Sleep(3000);
        }

        static void Main(string[] args)
        {
            Thread thread = new Thread(SeparateThread);
            thread.Start();

            Console.WriteLine("Main thread is waiting...");
            thread.Join();
            Console.WriteLine("SeparateThread is done...");

            Console.ReadLine();
        }
    }
}
However, if you have two threads in addition to the main thread and you want ThreadB to wait on ThreadA then there are numerous ways to do this. A common method is to just use an AutoResetEvent:
C#:
using System;
using System.Threading;

namespace ThreadingSynch
{
    class Program
    {
        static AutoResetEvent autoEvent = new AutoResetEvent(false);

        static void ThreadA()
        {
            Thread.Sleep(3000);

            autoEvent.Set();
        }

        static void ThreadB()
        {
            Console.WriteLine("Thread B is waiting on Thread A to complete...");
            autoEvent.WaitOne();
            Console.WriteLine("Thread A has completed...");
        }

        static void Main(string[] args)
        {
            Thread threadA = new Thread(ThreadA);
            threadA.Start();

            Thread threadB = new Thread(ThreadB);
            threadB.Start();
            
            Console.ReadLine();
        }
    }
}
 
Back
Top