views:

168

answers:

7

Hi,

I am having a function where I have used a thread in c#.net.

I am having a another function on the next line of that thread. But this function has to be called only after the thread gets executed.

How can i do it ?

Example..

Somefunction()
{
    // thread        //(thread started)
    add()            (another function but need to be executed only tha above thread gets over)
}
+2  A: 

Why start a separate thread if you want execution to be single threaded?

ck
It's a common example of starting an operation in the background, do something else in the mean while and then wait for that background operation to complete before proceeding.
Dave Van den Eynde
That makes sense, but its not what the OP is asking for
ck
Are you sure about that?
Dave Van den Eynde
I think so, he says he starts a thread, then runs something else when that thread is done.
ck
Well exactly, he probably starts up a second thread to do some background processing, and now wants to make sure it's over before stepping onto the add() call.
Dave Van den Eynde
@CK: I think @Dave is right on this point. The question did sound peculiarly put to me, but I think it means the same thing.
Cerebrus
+4  A: 

Use a BackgroundWorker and include the function call in the worker completeted event handler.

var worker = new BackgroundWorker();
_worker.DoWork += delegate { DoStuff(); };    
_worker.RunWorkerCompleted += worker_RunWorkerCompleted;
_worker.RunWorkerAsync();


[...]

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
   /// Do post-thread stuff
}
Unsliced
+1  A: 

by "after the thread gets executed", do you mean it must have started? or it must have finished?

If you mean finished, then you would typically Join() the thread - but there is no point Join()ing a thread you have stared in the line before (just execute the code directly). The other approach is to use a "callback" at the end of the threaded method.

If you mean started, then you can do things like:

object lockObj = new object();
lock(lockObj) {
    ThreadPool.QueueUserWorkItem(delegate {
        lock(lockObj) {
            Monitor.Pulse(lockObj);
        }
        // do things (we're on the second thread...)
    });
    Monitor.Wait(lockObj);
}
// thread has definitely started here
Marc Gravell
A: 

You can use , for instance, a ManualResetEvent. When you start the processing on the other thread, you call the reset method. When processing on the other thread has finished, you call set. Then, the method that must be executed on the 'main thread', needs to wait until the ManualResetEvent has been set before it can execute.

For more info, you can have a look at the ManualResetEvent at MSDN.

Frederik Gheysels
+3  A: 

Use Thread.Join to block the current thread until the specified thread has finished execution.

Dave Van den Eynde
A: 

If add() is thread safe, just call it at the end of the function you pass to create thread.

Pete Kirkham
A: 

You could try the following, it may not work for your scenario: I can't tell given the amount of detail you provided:

First create a delegate:

public delegate int MyThreadSignature(Something arg);

Then use the Begin/End Invoke pattern:

var thread = new MyThreadSignature(WorkerMethod);
thread.BeginInvoke(theArg, 
                   MyThreadEnded, /*Method to call when done*/, 
                   thread /*AsyncState*/);

Create the MyThreadEnded method:

void MyThreadEnded(IAsyncResult result)
{
    var thread = (MyThreadSignature)result.AsyncState;
    var result = thread.EndInvoke(result);
    // Call your next worker here.
}

The method to call MUST have the signature in the example: Name(IAsyncResult result).

Jonathan C Dickinson