views:

229

answers:

4

Somewhere in the code, a waitHandle is used to perfom some actions. However the thing with waithandle is that the form freezes while waiting for some action to complete. So the following code would not work:

frmProgressBar.show();
int successOrFail = PerformSynchronousActionUsingWaitHandle();
frmProgressBar.close();
frmMainScreen.show();

It won't work, since the frmProgressBar would be frozen instead. I really need to keep line #1, line #3 and line #4, but how do I rewrite PerformSynchronousActionUsingWaitHandle() such that the operation is still synchronous but the progress bar is displayed. I may be able to get around this by showing the progress bar on a different thread, but the design of the system is such that this would be very messy.

+1  A: 

Can't you use the BackgroundWorker Class? http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Gonzalo Quero
A: 

Use two different threads to handle this... one thread for progress bar and one thread for PerformSynchronousActionUsingWaitHandle() .. Hope this helps in some way

Samiksha
A: 

Have you tried using the BackgroundWorker class instead? You'll need to rewrite the method doing the synchronous task, though.

+1  A: 

you want to run the PerformSynchronousActionUsingWaitHandle in a thread so it doesn't lock up the ui thread.

    public event EventHandler<WorkCompleteArgs> WorkComplete;
    private void StartClick(object sender, EventArgs e)
    {
        //hook a complete event
        this.WorkComplete += new EventHandler(OnWorkComplete);

        //do start code

        //run the PerformSynchronousActionUsingWaitHandle process in a thread
        Thread thread = new Thread(new ThreadStart(PerformSynchronousActionUsingWaitHandle));
        thread.Start();
    }
    private void OnWorkComplete(object sender, EventArgs e)
    {
        //cross thread
        if (this.InvokeRequired)
        {
            this.BeginInvoke(new EventHandler(OnWorkComplete), new object[] { sender, e });
            return;
        }

        //un hook
        this.WorkComplete -= new EventHandler(OnWorkComplete);

        //do complete code

    }
    private void PerformSynchronousActionUsingWaitHandle()
    {
        Thread.Sleep(1000);
        if (WorkComplete != null)
        {
            //use event args to return the functions result if needed.
            WorkComplete(this, new WorkCompleteArgs(true));
        }
    }

    public class WorkCompleteArgs
    {
         public WorkState {get;private set;}
         public WorkCompleteArgs(bool workState)
         {
             this.WorkState = workState;
         }
    }
Hath
There's one problem. PerformSynchronousActionUsingWaitHandle() is supposed to return an error code. And in this case, StartClick() is supposed to return that error code. So, StartClick() has to wait for PerformSynchronousActionUsingWaitHandle() to finish before returning.
@Hao Wooi Lim - you can customise an event args to bring back any results. I've added this to the code above.
Hath