Link to home
Start Free TrialLog in
Avatar of pclarke7
pclarke7

asked on

c# keep form open after exiting program

I am new to c# and currently writing a barcoding application which reads through records grouped together by transaction name in a transaction file. Let say one such transaction called TRAN1 is made up of 30 transactions records sequenced 1 though to 30. In this transaction there are 4 required user inputs at sequence 2,10,20 & 25. A driver program (Forms application with form hidden) will read through each of the 30 records and at sequence 2,10,20 & 25 will call a User_Input form to take in the user input. The problem is that between sequence 2 & 10 there could be a delay of 5-10 seconds and the only form displayed is the User_Input. After each input is completed ,the form is closed when I return back to the Driver program making the whole process disjointed with delays between one input screen and the next input screen where nothing is displayed. Is there any way of keeping the User Input Screen Open when the Driver program is processing non Input sequences. I don't think a progress bar will work since most of the heavy listing is being done in the driver program which does not display a form. All suggestions welcome.
 
 regards
 Pat
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pclarke7
pclarke7

ASKER

thanks James
regards
Pat
Avatar of Mike Tomlinson
Based only on your title, I agree with JamesBurger.

But you said: "A driver program (Forms application with form hidden)"

So you have all of this running in a WinForms application?

If yes, then it is possible to achieve what you want...we'd just need to see your code and refactor it accordingly.

At a high level...

Display your form with the input controls disabled, and the 'X' to close the form disabled.
Start your processing work in a background thread.
When input is required, enable the form and wait for proper input.
You can make the background thread wait for input using thread synchronization techniques such as a ManualResetEvent().
After input is received, signal the thread to continue and disable the form again.

The BackroundWorker() control would be useful, though not necessary.

Let me know if you want to attempt this.
Thanks Idle Mind,
I'll go with James's suggestion as it is the simplier. However I am noticing flickering when the temp form is displayed with "Processing" message. Is there any way of minimizing this flicker.

regards
Pat
That really depends on how you designed the form, displayed the form, and whether you are updating it in anyway.

You could first try enabling the DoubleBuffered() property of that temp form and see if that helps.

Other than that we'd need to know a lot more information and possibly see code.
I have put together a simple example which illustrates how the screen flickers and the "processing please wait..." text does not appear on flickering Form3

Form 1 has a button called Start which when clicked will iterate from i from 0 to 99. When i is 10 or 20 an input screen (Form2) is called, otherwise a status screen (Form 3) is called. Form3 has a sleep(100) just to enable seeing the status screen. Appreciate any tips you can give regarding achieving a seemless transition between screens.

regards
Pat



 namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                //Display form as a modal dialog
                if (i == 10 || i == 20)
                {
                    this.Hide();
                    Form2 InputForm = new Form2();
                    InputForm.ShowDialog();
                }
                else
                {
                   
                    //Display form as a modal dialog
                    this.Hide();
                    Form3 StatusForm = new Form3();
                    StatusForm.ShowDialog();
                }
            }
            this.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}





namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}





namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(100);
            this.Close();
        }
    }
}
You are creating a new instance of  Form3 for every iteration.  Why not use the same instance over and over like this?
        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Application.DoEvents();
            Form3 StatusForm = new Form3();

            for (int i = 0; i < 100; i++)
            {
                //Display form as a modal dialog
                if (i == 10 || i == 20)
                {
                    StatusForm.Hide();
                    Application.DoEvents();

                    Form2 InputForm = new Form2();
                    InputForm.ShowDialog();

                    // ... other code? ...
                }
                else
                {
                    StatusForm.Show();
                    Application.DoEvents();

                    // ... other code? ...
                }
            }

            this.Show();
        }

Open in new window


You can get rid of the Sleep() call in Form3 then.