2016-06-29 28 views
0

嗨,大家好首先我想做这个循环。如何在if循环中返回“if”?

Process p = Process.GetProcessesByName("etcProgram.bin")[0]; 
       foreach (System.Diagnostics.ProcessModule moz in p.Modules) 
        if (csh.Text == "csh" || bin.Text == "bin") 
        { 
         if (moz.FileName.IndexOf("csh") != -1) 
         { 
          csh.Text = moz.BaseAddress.ToString(); 
         } 
         if (moz.FileName.IndexOf("bin") != -1) 
         { 
          bin.Text = moz.BaseAddress.ToString(); 
         } 
        } 
       else 
        { 
         !!!!!! return to "if" until "if code" happens !!!!!! 
        } 

但是我的不好的代码知识不能通过这个问题。所以我用定时器写了几乎相同的thinh。然后我写了这段代码。

private void tmrActive_Tick(object sender, EventArgs e) 
     { 
      try 
      { 
       Process p = Process.GetProcessesByName("Wolfteam.bin")[0]; 
       foreach (System.Diagnostics.ProcessModule moz in p.Modules) 
        if (csh.Text == "csh" || bin.Text == "bin") 
        { 
         if (moz.FileName.IndexOf("csh") != -1) 
         { 
          csh.Text = moz.BaseAddress.ToString(); 
         } 
         if (moz.FileName.IndexOf("bin") != -1) 
         { 
          bin.Text = moz.BaseAddress.ToString(); 
         } 
        } 
       else 
        { 
         tmrActive.Stop(); 
         MessageBox.Show("It's stopped"); 
        } 
      } 

但我看到MessageBox出现5-6次,当我开始这个。我不知道为什么。所以我不觉得使用这段代码非常安全。

1-你知道那个定时器有什么问题。此消息框不应该出现一次吗?

2-你可以帮我解释一下没有定时器的代码吗?无论如何要做吗?

+5

什么while循环您可以使用此整个代码的递归函数具有特定条件停止条件? – Charleh

+1

您错过了{在您的foreach之后并以else结尾}。这不是必需的,但它有助于可读性。并且它的每个模块都有一次是正式的,您在foreach模块中(在else中)这样做,因为if/else不依赖于模块(您正在检查bin.text和csh.text,但是不是moz),那么如果你有5-6个模块,你总是会有0或5-6个消息框 –

+4

“if”语句不是一个循环。似乎值得一提 – Jonesopolis

回答

2

你的意思是这样......

foreach (System.Diagnostics.ProcessModule moz in p.Modules) 
{ 
    bool breakloop = false; 
    while (!breakloop) 
    { 
     if (csh.Text == "csh" || bin.Text == "bin") 
     { 
      if (moz.FileName.IndexOf("csh") != -1) 
       csh.Text = moz.BaseAddress.ToString(); 

      if (moz.FileName.IndexOf("bin") != -1) 
       bin.Text = moz.BaseAddress.ToString(); 

      breakloop = true; 
     } 
    } 
} 
+1

请格式化您的代码。 – niksofteng

+1

我希望这是一个小清洁@ Think2ceCode1ce。 –

2

您只需使用break语句,以停止循环。

foreach (System.Diagnostics.ProcessModule moz in p.Modules) 
{ 
    if (csh.Text == "csh" || bin.Text == "bin") 
    { 
     if (moz.FileName.IndexOf("csh") != -1) 
     { 
       csh.Text = moz.BaseAddress.ToString(); 
     } 
     if (moz.FileName.IndexOf("bin") != -1) 
     { 
       bin.Text = moz.BaseAddress.ToString(); 
     } 
     break; 
    } 
} 

希望这会有所帮助。

+0

感谢它使用定时器:) – SodaX

0

,只要你想这样

foreach (System.Diagnostics.ProcessModule moz in p.Modules) { looping() { 


bool breakloop = false; 
while (!breakloop) 
{ 
    if (csh.Text == "csh" || bin.Text == "bin") 
    { 
     if (moz.FileName.IndexOf("csh") != -1) 
      csh.Text = moz.BaseAddress.ToString(); 

     if (moz.FileName.IndexOf("bin") != -1) 
      bin.Text = moz.BaseAddress.ToString(); 

     breakloop = true; 
     looping(); 
    } 
}