开发者

WPF: Non blocking UI

I have two checkboxes and a button. Under the button click handler,

private void button1_Click(..)
{
 if(checkbox1.true) { //start a process }
 if(checkbox2.true) { //start process 2 once process 1 is done}

}

Both process 1 and 2 are started asynchronously. How do i set the dependency between process 2 and process 1? I do not want to poll if process 1 is done before s开发者_如何学Ctarting process 2. That would block the UI. ANy other solution?

THanks


Implement a background worker to do process1

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

In your runworker completed event check to see if checkbox 2 is checked

//copy and pasted from msdn

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
                    if (e.Cancelled == true)
                    {
                        //dosomething
                    }
                    else if (e.Error != null)
                    {
                        //dosomething
                    }
                    else
                    {
                        if(checkbox2.Checked)
                        {
                           //fire off process 2
                         }
                    }
                }


You can hook up on the Exited event of the Process class.

Something along the lines of:

Process p = ...
if(checkbox2.Checked)
    p.Exited = ... // Event handler that starts process 2

p.Start()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜