你如何退出另一种方法?

时间:2013-04-18 14:11:18

标签: c# wpf windows methods return

无论如何你可以从其他方法中退出一个方法吗?

            if (workisdone())
            Xceed.Wpf.Toolkit.MessageBox.Show("Done");
            return.workisdone(); // or something like that?????
            DispatcherTimer timer = (DispatcherTimer)sender;
            timer.Stop();
            timer = null;
        }
    }

4 个答案:

答案 0 :(得分:3)

if (workisdone())
{
    Xceed.Wpf.Toolkit.MessageBox.Show("Done");
    return;
}
DispatcherTimer timer = (DispatcherTimer)sender;
timer.Stop();
timer = null;

答案 1 :(得分:2)

除非您在某处发生多线程,否则在您退出workisdone()方法之前,实际上无法输入if块...

答案 2 :(得分:1)

    bool res = workisdone();
    if(res)
       return res;
   //rest of code here - any code below here will not run if workisdone() returns true

我通常会假设您在退出循环之前停止计时器,但此代码看起来就像您想要的那样。

答案 3 :(得分:0)

好的,我明白你要做什么了。所以用这种方式修改你的代码:

var isWorkDone = workisdone();

// i think, you should stop the timer no matter what the workisdone() result is.
DispatcherTimer timer = (DispatcherTimer)sender;
timer.Stop();
timer = null;

// pop the msg only if workisdone() == true
if (isWorkDone)
{
  Xceed.Wpf.Toolkit.MessageBox.Show("Done");
}

return isWorkDone; // will return pass or fail.