Solved

How to restart a Visual Studio Windows Form

Posted on 2014-09-05
7
1,705 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Set cookies HttpOnly and Secure 4 52
Change data in datatable 8 15
Why is "packages element is not declared"? 2 39
Create XML 5 33
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now