?
Solved

How to restart a Visual Studio Windows Form

Posted on 2014-09-05
7
Medium Priority
?
2,170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

752 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