Link to home
Start Free TrialLog in
Avatar of MBarongan
MBaronganFlag for United States of America

asked on

How to restart a Visual Studio Windows Form

I have an ASP.NET web form application with a button in "Default.aspx" that restarts the application like this.

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.ToString());
        }

I created a .NET window form version of this program, but I can't get the button in "Form1.cs [Design]" to restart the application. Here are the C# commands I've tried so far in the Code Behind:

        protected void Button1_Click(object sender, EventArgs e)
        {
            Application.Restart();
            Application.Run();
            Refresh();
        }

What method in the Button1_Click event will close the window (Form1) and reopen it, effectively rerunning the application?
Avatar of Ramkisan Jagtap
Ramkisan Jagtap
Flag of Finland image

Pleade refer folloein link
stackoverflow.com/questions/10814944/how-to-restart-windows-form-application-without-exiting-and-reloading-in-c
Avatar of MBarongan

ASKER

In that stackoverflow article, the person asking the question got farther than I did with Application.Restart(). He said Application.Restart() "would just restart the application but does not run the rest". But for me, Application.Restart() did absolutely nothing. My program was totally unresponsive to the button-click when I used Application.Restart() in the button's event handler. Is there a better method to use in the button's event handler?
Application.Restart alone is sufficient.

Your Application.Run and Refresh calls are useless because the new instance triggered by the Restart will not answer to these lines of code. In fact, most of the time, Application.Run will cause an error when called from a Form.

You might also have code in other events that prevents the Restart. For instance, setting e.Cancel to True in the FormClosing event can lead to that happening.

Finally, if it works with the logic of your application, you could launch a new instance of your Form and close the current one. In some instances, it might be a better choice because it does not even involve a restart of the application, you stay in the same run:

YourForm frm = new YourForm();
this.Close()
The first scenario that you mentioned is for Web application. It works on HTTP which is stateless. So while using
Response.Redirect

Open in new window

It tell the server that this is new request (more appropriate would be fresh request from client and don't consider that as old one.).

Windows and Web two are different scenario(s). (Are you using that line of code for resting your application to initial state after some operation :-?

As James suggested,
You need to set a logic for that.
From your MDI or parent form, you need to set closing event for the form, under which you can execute same line of code that's provided by James.

** If you will keep code provided by James under same form  (like Form-1 is currently opened and you want to re-open that form) then in that case it won't really work as the parent object (or where you are creating new object) is already displosed via
this.close();

Open in new window


hope this would help.
ASKER CERTIFIED SOLUTION
Avatar of MBarongan
MBarongan
Flag of United States of America 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
Great! Sometime a silly mistake converts into night mare!
Creating a new event handler resolved the issue.