Solved

How to restart a Visual Studio Windows Form

Posted on 2014-09-05
7
1,922 Views
Last Modified: 2014-09-11
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?
0
Comment
Question by:MBarongan
7 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40307070
Pleade refer folloein link
stackoverflow.com/questions/10814944/how-to-restart-windows-form-application-without-exiting-and-reloading-in-c
0
 

Author Comment

by:MBarongan
ID: 40307112
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?
0
 
LVL 40
ID: 40307128
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()
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:ItWorked
ID: 40307272
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.
0
 

Accepted Solution

by:
MBarongan earned 0 total points
ID: 40307415
I figured out the problem. The event handler for Button1 was copied and pasted into my project from another project. So it wasn't actually wired to the button. I deleted the event handler, double-clicked on Button1 to create a new event handler, and entered Application.Restart() in the new event handler. It works now.

Thank you everyone for your input.
0
 
LVL 4

Expert Comment

by:ItWorked
ID: 40307417
Great! Sometime a silly mistake converts into night mare!
0
 

Author Closing Comment

by:MBarongan
ID: 40316490
Creating a new event handler resolved the issue.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question