locked
infinite loop implemenaion RRS feed

  • Question

  • Hi, my objective is to create a window form application that implements an in infinite-loop. The attached listing is a form with 2 buttons to simulates a counter that increments when the up button is clicked and decrements with the clicking of the down button. I placed the infinite loops in the button-clicking procedures but there was no display at the window. Further, when I suppressed the action of the infinite loops in the procedure, the window displays the fresh count only after I minimize the window then maximize again. It is appreciated if anyone could give me a lead about how to implement the infinite loop properly. Thanks.

    using System;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    public class WinForm : Form
    {
        private Button b1;
        private Button b2;
        uint counter = 0;// printing counter
        String CntStr;
        String msgStr;
        bool Up, Down;
        public WinForm()
        {
            InitializeComponent();
        }
        public bool body()
        {
            if (     Up   && (counter < 10000)) counter++;
            else if (Down && (counter >     0)) counter--;
            CntStr = counter.ToString();
            msgStr = "counter reading = " + CntStr;
            this.Paint += f1_paint;
            return true;
        }
        public void b1_click(object sender, EventArgs e)
        {
            Up = true;
            Down = false;
            //while (true){
                body();
            //}
        }
        public void b2_click(object sender, EventArgs e)
        {
            Down = true;
            Up = false;
            //while (true){
                body();
            //}
        }
        private void InitializeComponent()
        {
            this.b1 = new Button();
            this.b1.Click += new EventHandler(b1_click);
            b1.Location = new Point(0, 40);
            this.b1.Text = "UP";
            this.Controls.Add(b1);
            this.b2 = new Button();
            this.b2.Click += new EventHandler(b2_click);
            b2.Location = new Point(0, 80);
            this.b2.Text = "DOWN";
            this.Controls.Add(b2);
            // 
            // WinForm
            // 
            this.Paint += new PaintEventHandler(f1_paint);
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "WinForm";
        }
        private void f1_paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawString(msgStr, new Font("Helvetica", 10),
            new SolidBrush(Color.Blue), 40, 100);
        }
    }
    public class Program
    {
        public static int Main()
        {
            Application.Run(new WinForm());
            return 0;
        }
    }


    Esmat

    Thursday, January 10, 2013 4:46 PM

Answers

  • Hi, my objective is to create a window form application that implements an in infinite-loop. The attached listing is a form with 2 buttons to simulates a counter that increments when the up button is clicked and decrements with the clicking of the down button. I placed the infinite loops in the button-clicking procedures but there was no display at the window. Further, when I suppressed the action of the infinite loops in the procedure, the window displays the fresh count only after I minimize the window then maximize again. It is appreciated if anyone could give me a lead about how to implement the infinite loop properly. Thanks.

    Hi Esmat,

    You can call Form.Invalidate() method to redrawn the form. Therefore the number will be refreshed.

    To count down/up the number,  you need to create another thread. A Timer or backgroundworker could be a good choice, see http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

    Best Regards,


    Bob Wu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by EsmatB Friday, January 11, 2013 4:58 PM
    Friday, January 11, 2013 7:10 AM
  • Hi,

    You should be aware that a windows forms based application is event based. A loop is running that checks for new events and processes it.

    If you do not finish the processing of an event further events cannot be processed. So the window is not redrawn as one result.

    Simply use an timer to do ther counting or do it on another thread e.g through a background worker.

    With kind regards,

    Konrad

    • Marked as answer by EsmatB Friday, January 11, 2013 4:58 PM
    Friday, January 11, 2013 10:05 AM

All replies

  • Hi, my objective is to create a window form application that implements an in infinite-loop. The attached listing is a form with 2 buttons to simulates a counter that increments when the up button is clicked and decrements with the clicking of the down button. I placed the infinite loops in the button-clicking procedures but there was no display at the window. Further, when I suppressed the action of the infinite loops in the procedure, the window displays the fresh count only after I minimize the window then maximize again. It is appreciated if anyone could give me a lead about how to implement the infinite loop properly. Thanks.

    Hi Esmat,

    You can call Form.Invalidate() method to redrawn the form. Therefore the number will be refreshed.

    To count down/up the number,  you need to create another thread. A Timer or backgroundworker could be a good choice, see http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

    Best Regards,


    Bob Wu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by EsmatB Friday, January 11, 2013 4:58 PM
    Friday, January 11, 2013 7:10 AM
  • Hi,

    You should be aware that a windows forms based application is event based. A loop is running that checks for new events and processes it.

    If you do not finish the processing of an event further events cannot be processed. So the window is not redrawn as one result.

    Simply use an timer to do ther counting or do it on another thread e.g through a background worker.

    With kind regards,

    Konrad

    • Marked as answer by EsmatB Friday, January 11, 2013 4:58 PM
    Friday, January 11, 2013 10:05 AM
  • Bob and Konrad,

    Thanks a lot for your kind replies. This exercise was my first brush with C# forms and I think I have learned a lot from your answers. Thanks again


    Esmat

    Friday, January 11, 2013 5:01 PM