Hey hi i am new to this field , i am working on project which is desktop application in C#,in the project there are three forms ,i want to show only working form and all other form should get close 'showdialogue' method also does not work ...............please give me some solution for this .

Dani AI

Generated

Short diagnosis: hiding Form1 will not end the WinForms message loop if the app started with Application.Run(Form1), and any foreground threads or timers left running will keep the process alive in Task Manager. and pointed at hiding/closing behavior; suggested threading — that thread-based watcher (with repeated Thread.Start/Thread.Sleep) is the likely culprit because it spawns foreground threads that never terminate.

Practical fixes (pick one):

  • Quick fix: when you open Form2, attach a handler to Form2.FormClosed that closes the hidden Form1 (or calls Application.Exit()), so the message loop can end when the visible window closes:

    var f2 = new Form2();
    f2.FormClosed += (s,e) => this.Close(); // or Application.Exit();
    f2.Show();
    this.Hide();
  • Better pattern: use a custom ApplicationContext to control application lifetime instead of tying it to a single form. This lets the app exit when you explicitly decide (e.g., when last form is closed). See ApplicationContext docs for details (ApplicationContext, Application.Run overload).

Threading cautions: if you must use threads, make them background threads (thread.IsBackground = true) or use the Task-based API with cancellation tokens. Avoid busy loops or recursive Thread.Start calls — they keep the process alive. Also check for timers, BackgroundWorker instances, or other long-running work that wasn’t stopped.

Debug checklist: attach the debugger or use Process Explorer/VS Threads window to see live threads; call Application.OpenForms.Count to confirm open forms; try replacing the thread solution with the FormClosed approach to verify the app exits correctly. For reference on form lifecycle events see Form.FormClosed.

Recommended Answers

All 7 Replies

How you are going to open these 3 forms? Will you open 1 and then by clicking something on form1, this has to close and open form2, and so on?

1. You can simply use hide() method for the form you want NOT to show anymore (ps: but it has to be opened before).
2. You can open form2, and just after that you can call form1.Close() method.

yes i am opening the forms in the same way........

use Threading.. i can help you if you don't know how

private void button1_Click(object sender, EventArgs e)
        {
//showing the form 2
Form form2 = new Form();
form2.show();
//hiding the form 1:
this.hide();
        }

if you use the show dialog, you cant hide the form1 afaik.

but how to close hidden form that we have hidden ,because on form2 i do not have button to go back

at the top of Form1 write:

using System.Threading;

after that make a button that open Form2 like this:

private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show(); this.Hide();
            new Thread(new ParameterizedThreadStart(IsOpen)).Start(Prodacts);
        }

and write also this in Form1 after the Button Code:

delegate void FormShow(object onj);
        private void IsOpen(object obj)
        {
            bool isFormOpen = false;
            foreach (Form frm in Application.OpenForms)
                if (frm.Equals((Form)obj))
                    isFormOpen = true;
            if (!isFormOpen)
            {
                if (this.InvokeRequired)
                    this.Invoke(new FormShow(IsOpen), obj);
                else
                    this.Close();
            }
            else
            {
                Thread.Sleep(0);
                new Thread(new ParameterizedThreadStart(IsOpen)).Start((Form)obj);
            }
        }

it is working but there is still problem of closing hidden form ....application remains open in task manager how to close hidden form?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.