views:

62

answers:

2

I have an external library which has a method which performs a long running task on a background thread. When it's done it fires off a Completed event on the thread that kicked off the method (typically the UI thread). It looks like this:

public class Foo
{
    public delegate void CompletedEventHandler(object sender, EventArgs e);
    public event CompletedEventHandler Completed;

    public void LongRunningTask()
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        bw.RunWorkerAsync();
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        Thread.Sleep(5000);
    }

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (Completed != null)
            Completed(this, EventArgs.Empty);
    }
}

The code that calls this library looks like this:

private void button1_Click(object sender, EventArgs e)
{
    Foo b = new Foo();
    b.Completed += new Foo.CompletedEventHandler(b_Completed);
    b.LongRunningTask();

    Debug.WriteLine("It's all done");    
}

void b_Completed(object sender, EventArgs e)
{
    // do stuff
}

In the button1_Click method, after I call b.LongRunningTask(), the Completed event fires off 5 seconds later on the UI thread, I update the UI and everything is great, since I don't have to deal with marshaling stuff to the proper thread.

However, I now have a need for the process to be synchronous (without changing the external library). In other words, after I kick off .LongRunningTask method, the next meaningful statement in that method should fire after .LongRunningTask has completed.

I've tried doing it with the EventWaitHandle (e.g. doing WaitOne after the call to LongRunningTask and then Resetting it in the Completed event, but that just locks everything up).

Is there a method in the .NET framework that allows me to do this?

+3  A: 

I've tried doing it with the EventWaitHandle (e.g. doing WaitOne after the call to LongRunningTask and then Resetting it in the Completed event, but that just locks everything up).

That is exactly what will happen if you make this synchronous, by definition. You can't make it synchronous without blocking the UI thread.

Instead of having "the next meaningful statement in that method" fire after the operation, you'll need to either make it blocking, or have the meaningful statement fire in the callback.

Reed Copsey
If I block the UI thread, the BackgroundWorker in the library won't be able to fire the RunWorkerCompleted event, which occurs on the UI thread. Thus the app will be hung.
AngryHacker
Background worker was originally built with an idea that it could be used from the UI tier to fire off async processes that would then pick up at the completed event.
Jim Leonardo
@AngryHacker: Unless you unblock before then, in the background thread.
Reed Copsey
@Jim: Yeah - using BW in a synchronous scenario is just making pain.
Reed Copsey
A: 

Create new thread with it's message pump. Call your BW from that thread. Set the BW callback to raise some flag that the thread will monitor in a loop which will do:

Application.DoEvents();
Thread.Sleep(0);

Join() your thread from the main UI thread.

Hm, maybe no new thread is needed here, only DoEvents()?

Daniel Mošmondor