locked
The Best Place For Loops? RRS feed

  • Question

  • I have a C# program in which it runs a form, and in the form it checks for information constantly to a server. So after the user logs in, I want to close the form, but I don't want the loop to stop. I get this weird bug where I make the new form show the information of the account, but it stops half way, and gives the circling icon when you hover over it and the login form never closes. I am positive its the loop. I want to have the loop in the Program.cs, But if I do that I have a difficult time gathering data from the form.

    So, I was wondering if I could still have it run even if its closed, or can someone teach me how to gather information from a form from the Program.cs?

    Sunday, March 30, 2014 2:26 AM

Answers

  • A form is a class and you don't have to dispose the form when it closes.  You can create instances of the form so two forms can be active at the same time.  See if you understand the code below.

    Form1

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            Form2 form2;
            public Form1()
            {
                InitializeComponent();
                form2 = new Form2(this);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                form2.Show();
            }
        }
    }
    

    Form2

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            Form1 form1;
            public Form2(Form1 nform1)
            {
                InitializeComponent();
                form1 = nform1;
                this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
            }
    
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                e.Cancel = true;
                this.Hide();
            }
    
    
        }
    }


    jdweng

    • Marked as answer by Fred Bao Tuesday, April 8, 2014 3:04 AM
    Sunday, March 30, 2014 8:51 AM
  • You want to make an application wide resource - the data wich is regulary polled by a for loop and the for-loop itself should be a class instance that all forms share/is idependant of any form.
    Unlike WPF, WinForms never got a proper support for Application wide varriables, so you have to create a fix with a static class:

    http://stackoverflow.com/questions/1293926/c-sharp-winforms-global-variables

    http://bytes.com/topic/c-sharp/answers/246920-global-application-variables-winforms-app

    A more advanaced technique would be to have one non-static class for the data and the loop. Plus a second, static class that can hold multiple instances (each in a seperate field) of this data/loop class. That way you can have application wide resources without running into normal limitations of static.
    There are otehr appraoches like Singleton Pattern or jsut relaying the isntance via the a overloaded Constructor.

    Change Notification might be worth a look for this, as it simplyfies cases where you have many stakeholders in changes to a value/property:
    http://msdn.microsoft.com/en-us/library/ms229614.aspx


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


    • Edited by Christopher84 Sunday, March 30, 2014 2:37 PM
    • Marked as answer by Fred Bao Tuesday, April 8, 2014 3:04 AM
    Sunday, March 30, 2014 2:35 PM

All replies

  • A form is a class and you don't have to dispose the form when it closes.  You can create instances of the form so two forms can be active at the same time.  See if you understand the code below.

    Form1

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            Form2 form2;
            public Form1()
            {
                InitializeComponent();
                form2 = new Form2(this);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                form2.Show();
            }
        }
    }
    

    Form2

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            Form1 form1;
            public Form2(Form1 nform1)
            {
                InitializeComponent();
                form1 = nform1;
                this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
            }
    
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                e.Cancel = true;
                this.Hide();
            }
    
    
        }
    }


    jdweng

    • Marked as answer by Fred Bao Tuesday, April 8, 2014 3:04 AM
    Sunday, March 30, 2014 8:51 AM
  • You want to make an application wide resource - the data wich is regulary polled by a for loop and the for-loop itself should be a class instance that all forms share/is idependant of any form.
    Unlike WPF, WinForms never got a proper support for Application wide varriables, so you have to create a fix with a static class:

    http://stackoverflow.com/questions/1293926/c-sharp-winforms-global-variables

    http://bytes.com/topic/c-sharp/answers/246920-global-application-variables-winforms-app

    A more advanaced technique would be to have one non-static class for the data and the loop. Plus a second, static class that can hold multiple instances (each in a seperate field) of this data/loop class. That way you can have application wide resources without running into normal limitations of static.
    There are otehr appraoches like Singleton Pattern or jsut relaying the isntance via the a overloaded Constructor.

    Change Notification might be worth a look for this, as it simplyfies cases where you have many stakeholders in changes to a value/property:
    http://msdn.microsoft.com/en-us/library/ms229614.aspx


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


    • Edited by Christopher84 Sunday, March 30, 2014 2:37 PM
    • Marked as answer by Fred Bao Tuesday, April 8, 2014 3:04 AM
    Sunday, March 30, 2014 2:35 PM