1

It is possible to run infinite loop , but not block program or form with it?

For example i have one infinite loop

private void infLoop()
    {
      while(1<3)
      {
          textBox1.Text+="1";
          textBox1.Refresh();
      }
    }

Then on button I call it.

    private void button1_Click(object sender, EventArgs e)
    {
        infLoop();
    }

This wold block form , I cannot close it, move it, etc..

Is there some way who I run something like this in backward , and not block form with it?

Thanx

4
  • Use a Timer/Thread/ThreadPool/Task for the loop, with a synchronization mechanism to update your UI. Commented Jul 11, 2014 at 6:39
  • A timer or multithreading is probably what you are searching for. Commented Jul 11, 2014 at 6:50
  • @TheMotivation please don't move him towards threading...its going to cause more issues than its worth. Commented Jul 11, 2014 at 6:58
  • @Aron you're probably right. He's working with winforms, so it's possible that he'll have to invoke a lot of components. Commented Jul 11, 2014 at 7:30

4 Answers 4

4

Simplest way to achieve this is by using async await. However this will run way too fast and will kill your computer.

private async void infLoop()
{
    while(1<3)
    {
        await Task.Yield();
        textBox1.Text+="1";
        textBox1.Refresh();
    }
}

What you are more likely wanting is this...

private async void infLoop()
{
    int i = 1;
    while(true)
    {
        await Task.Delay(TimeSpan.FromSeconds(1));
        textBox1.Text = i.ToString();
        textBox1.Refresh();
        i += 1;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 this technique is also useful for implementing "background infinite loops" like the ones typically used by background thread that loops around forever and performs a task when possible. Using async/await has the advantage that you don't have to create an extra dedicated thread and whatever thread is used for the infinite loop can do other things when waiting for more tasks to arrive - pretty damn cool!
1

Based on msdna, try this solution: maybe you'd have to adapt it for your needs.

This solution use a new Thread for setting textbox and a callback to avoid "the cross-thread InvalidOperationException" raised when you try to modify a form with the same thread which run the form itself.

    private void Form1_Load(object sender, EventArgs e)
    {
        System.Threading.Thread newThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadProcSafe));
        newThread.Start();

    }

    private void ThreadProcSafe()
    {
        this.SetText();
    }

    private void SetText()
    {
        if (textBox1.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d);

        }
        else
        {
            while (1 < 3)
            {
                textBox1.Text += "1";
                textBox1.Refresh();
            }
        }
    }

1 Comment

Closest piece of code to achieve the work I've seen here...but bit over the top...especially with modern async await.
0
  1. Drag the backgroundworker control from tool box.
  2. double click on it
  3. add infinite loop in DoWorkEvent(autogenerated) add this code in the LoadFunction

        if (backgroundWorker1.IsBusy == false)
        {
            backgroundWorker1.RunWorkerAsync();
        }
    

    Backworkworker is a thread that runs in background to do heavy task and it runs GUI concurrently, without getting freeze .unless you use system calls etc

Comments

-2

You are talking about winforms? See BackgroundWorker

1 Comment

If you even say its a dirty way... why even suggest it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.