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
Solved

How to restart a Visual Studio Windows Form

Posted on 2014-09-05
7
1,858 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

792 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