Innsolit
Innsolit

Reputation: 19

How to return "if" in a if loop?

Hi guys actually first I wanted to do this loop.

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 !!!!!!
                    }

But my poor code knowledge can't come through this problem. So I wrote nearly same thinh with timer. Then I wrote this code.

 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");
                    }
            }

But I saw that MessageBox appears 5-6 times when I started this.And I dont know why. So I dont feel very safe about use this code.

1- Do you know what's the problem with that timer. Shouldn't this messagebox appear once?

2- Can you help me about the code without timer.Is there anyway to do it?

Upvotes: 0

Views: 180

Answers (3)

Umair Ahmed
Umair Ahmed

Reputation: 562

You could use this whole code as a Recursive function with a Specific condition to stop the condition whenever you want like this

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();
    }
}

Upvotes: 0

guijob
guijob

Reputation: 4488

You can simply use break statement in order to stop a loop.

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;
     }
}

Hope this helps.

Upvotes: 2

Rick Runowski
Rick Runowski

Reputation: 355

You mean something like...

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;
        }
    }
}

Upvotes: 2

Related Questions