0

if I start my C# Windows Form Application, buttons hangs, because it is endless cycle. I want to see the change of variable value from button2 click into infinite loop of button1 through the global variable

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XX_5
{
    public partial class Form1 : Form
    {
        private int g;

        public Form1()
        {
            InitializeComponent();
        }   
        private void button1_Click(object sender, EventArgs e)
        {           
            int i = 0;

            for (;;)
            {   
                textBox1.AppendText("ID: [" + i++ + "] Variable value: [" + g + "]\n");    
            }
        }    
        private void button2_Click(object sender, EventArgs e)
        {
            g = 1;
        }
    }
}
  • @Grant Winney hello, can you show me on some example how to do the same but right way, infinite loop in GUI and value from button2 to process inside button1 I'm just want to change it when button2 is pressed –  Aug 20 '16 at 01:29
  • 1
    The question is too broad, especially given how many Q&A already exist on Stack Overflow that address the basic concept of **not blocking the UI thread**. There's only one thread that handles drawing the UI. If you don't let that thread do anything else, because you're stuck in an infinite loop, then _drawing the UI won't happen_. See the marked duplicate for some advice on how to deal with this. – Peter Duniho Aug 20 '16 at 02:20
  • 1
    For .NET 4.5, I recommend `Task.Run()` to start an operation, and `Progress` to provide any relevant progress reporting back to the main thread. Alternatively, you can use `async`/`await` in a loop, where you `await` a `Task.Run()` operation for each iteration of the loop. – Peter Duniho Aug 20 '16 at 02:21
  • The simplest thing is to put `Application.DoEvents();` in the loop. *In general* though, `Application.DoEvents()` is a considered a bit of a hack. Using `Task` and `async/await` will usually lead to a more maintainable design. – Dax Fohl Aug 07 '17 at 16:53

2 Answers2

-1

If you keep the infinite-loop in the button-click event handler, your application hangs definitely, since windows can't get / dispatch /process messages any more.

You can fix like the following:

private void button1_Click(object sender, EventArgs e)
    {           
        int i = 0;
        textBox1.AppendText("ID: [" + i++ + "] Variable value: [" + g + "]\n");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        // int g = 1; // here you declare a local variable
        g = 1;  // use the member variable instead
    }
  • 1
    "i" should also be a member. – gopher2008 Aug 20 '16 at 01:28
  • yes now variable works but how to see this change of variable gotten from button2 click in my endless cycle, how to make button work –  Aug 20 '16 at 02:09
  • The original code clearly shows a loop, but there is no loop in this answer, making this suggestion completely inappropriate in this case. – Peter Duniho Aug 20 '16 at 02:40
-1

Take a look at the Timer control (under the Components tab in the toolbox). You can put your code (without the for loop) in there and it will run every x milliseconds with the benefit that it will not hang. You will need to define your i variable at the form level when you do this. Your timer can then access this 'global variable'.

Something like this...

public partial class Form1 : Form
{

    private int i = 0;
    private int g = 0;
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        g = 1;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        textBox1.AppendText("ID: [" + i++ + "] Variable value: [" + g + "]\n");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }
}
  • not really understand how timer works for this case, can you show me some example, important thing for me to get this endless loop inside and see the change of variable gotten from button2 click –  Aug 20 '16 at 02:06
  • Nothing in the original code involves periodic delay between operations, so a timer is a completely inappropriate suggestion in this case. – Peter Duniho Aug 20 '16 at 02:39
  • If an infinite loop does not yield to the CPU then a timer is the easiest way for a C# learner to fix the problem. The point is not to periodically delay - the point is to *yield*. The question author is obviously trying to learn C# by trying small tasks - learning threading at this stage would be inappropriate. – Jason Werry Aug 20 '16 at 07:57