【问题标题】:How do you exit from another method?你如何从另一种方法中退出?
【发布时间】:2013-04-18 14:11:17
【问题描述】:

你是否可以像这样从另一个方法中退出一个方法?

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

【问题讨论】:

  • 请说明您想要实施的想法。
  • 我不明白你想要达到什么目的。如果该值符合特定条件,您想返回另一个方法的值吗?

标签: c# wpf windows methods return


【解决方案1】:
if (workisdone())
{
    Xceed.Wpf.Toolkit.MessageBox.Show("Done");
    return;
}
DispatcherTimer timer = (DispatcherTimer)sender;
timer.Stop();
timer = null;

【讨论】:

  • 如果workisdone() == true,计时器将永远不会停止和处置。这是正确的行为吗?
  • @code4life 超出了我认为的问题范围。但好点!
  • 其实我认为是在范围内,因为这里的内存会流血相当致命。
【解决方案2】:

除非你在某处发生了一些多线程,否则在你已经退出 workisdone() 方法之前,你不能真正进入 if 块......

【讨论】:

    【解决方案3】:
        bool res = workisdone();
        if(res)
           return res;
       //rest of code here - any code below here will not run if workisdone() returns true
    

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

    【讨论】:

      【解决方案4】:

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

      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.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-29
        • 2014-08-14
        • 2016-02-11
        • 1970-01-01
        相关资源
        最近更新 更多