James Morrish
James Morrish

Reputation: 475

How can i run 'while' statement every x seconds

I'm checking if a process is running and if not start that process. It works fine, but only once, after that, the statement is finished and doesn't run again, which is expected.

but I can't figure out how to run it every X seconds.

this is the code I have:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        while (checkBox1.Checked)
        {

            var retVal = Process.GetProcesses().Any(p => p.ProcessName.Contains(textBox1.Text));

            if (retVal.Equals(true))
            {
                listBox1.Items.Add(textBox1.Text + @" " + @"is running." + @" " + DateTime.Now);
                return;
            }

             if (retVal.Equals(false))
             {
                    listBox1.Items.Add(textBox1.Text + @" " + @"is not running, attempting to start." + DateTime.Now);
                    Process.Start(textBox2.Text);
                    return;
             }
        }
    }

Now I know the return statement at the end of each if statement will stop the code, but if I don't have that it will crash the app because it will just write continually to the list box.

How can I run the code

while (checkBox1.Checked)

every X seconds?

Upvotes: 0

Views: 1260

Answers (2)

MetaColon
MetaColon

Reputation: 2871

You probably use WinForms, your code looks very much like that, but as it isn't in your question, I'll add another solutin to OmerCDs.

You could use Task.Delay. However, this hibernates the thread, therefore it shouldn't be run on the main thread. You could use a code like this:

    Task.Run(async () => 
    {
        while (true)
        {
            while (checkBox1.Checked)
            {
                var retVal = Process.GetProcesses().Any(p => p.ProcessName.Contains(textBox1.Text));
                if (retVal.Equals(true))
                {
                    listBox1.Items.Add(textBox1.Text + @" " + @"is running." + @" " + DateTime.Now);
                    return;
                }
                if (retVal.Equals(false))
                {
                    listBox1.Items.Add(
                        textBox1.Text + @" " + @"is not running, attempting to start." + DateTime.Now);
                    Process.Start(textBox2.Text);
                    return;
                }
            }
            await Task.Delay(2 /*your waiting time in seconds*/ * 1000);
        }
    });

Upvotes: 1

OmerCD
OmerCD

Reputation: 46

You can use timer to check it. It is really simple.

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (!checkBox1.Checked) return;
        var retVal = Process.GetProcesses().Any(p => p.ProcessName.Contains(textBox1.Text));
        if (retVal.Equals(true))
        {
            listBox1.Items.Add(textBox1.Text + @" " + @"is running." + @" " + DateTime.Now);
            return;
        }

        if (!retVal.Equals(false)) return;
        listBox1.Items.Add(textBox1.Text + @" " + @"is not running, attempting to start." + DateTime.Now);
        Process.Start(textBox2.Text);
    }

And you can choose your interval from properties of timer. Remember it is miliseconds (1000ms = 1 second).

enter image description here

Upvotes: 1

Related Questions