如何退出另一个方法
本文关键字:另一个 方法 退出 何退出 | 更新日期: 2023-09-27 18:09:46
是否可以像这样从另一个方法退出一个方法?
if (workisdone())
Xceed.Wpf.Toolkit.MessageBox.Show("Done");
return.workisdone(); // or something like that?????
DispatcherTimer timer = (DispatcherTimer)sender;
timer.Stop();
timer = null;
}
}
if (workisdone())
{
Xceed.Wpf.Toolkit.MessageBox.Show("Done");
return;
}
DispatcherTimer timer = (DispatcherTimer)sender;
timer.Stop();
timer = null;
除非你有一些多线程发生的地方,你不能真正进入if块,直到你已经从workisdone()
方法退出…
bool res = workisdone();
if(res)
return res;
//rest of code here - any code below here will not run if workisdone() returns true
我通常会假设你在退出循环之前停止一个计时器,但这段代码看起来像你想要的。
好了,我明白你的意思了。所以这样修改你的代码:
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.