Senthil
Senthil

Reputation:

Execute a statement after thread gets over

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

Upvotes: 1

Views: 4316

Answers (7)

Unsliced
Unsliced

Reputation: 10552

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
}

Upvotes: 5

Jonathan C Dickinson
Jonathan C Dickinson

Reputation: 7285

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

Upvotes: 0

Pete Kirkham
Pete Kirkham

Reputation: 49311

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

Upvotes: 0

Dave Van den Eynde
Dave Van den Eynde

Reputation: 17405

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

Upvotes: 4

Frederik Gheysels
Frederik Gheysels

Reputation: 56934

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.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062770

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

Upvotes: 1

cjk
cjk

Reputation: 46425

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

Upvotes: 2

Related Questions