'Application.Restart' not working in ClickOnce deployed application [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is Application.Restart() not reliable?
I pulled the code straight from MSDN. This updates my application, but Restart() does not work. The application shuts down, but it does not restart.
I added a MenuItem to my Form to validate that Restart() works at all:
private void restartToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
This will restart the application (of course, it performs no updates and is user initiated, so it is fairly useless).
I have nothing else going on with this application. No event handlers for the Form on shutdown, nothing. This is the most basic Windows Forms application I could build (it just displays a resource JPEG in an ImagePanel).
Why does Restart() not work here?

Is your application Windows Forms or WPF? Because Application.Restart only exists in the Windows Forms Application object (System.Windows.Forms.Application) and is not supported by applications running under the WPF Application (System.Windows.Applications). You can still call it, but as the application context is different, it doesn't work.

If you are using a Mutex, or something of the like to ensure only one instance of the application is running at a time, that be causing this issue.

Try wrapping it with a BeginInvoke just in case it's not on the main STA thread.

Are you sure that you're calling Application.Restart from the main form? If you call a form with .ShowDialog and then from that form call Application.Restart, it won't work because the .ShowDialog causes the dialog form to run on a separate thread.

Try to raise a new process, maybe that can workaround it:
Process.Start(Application.ExecutablePath);

Related

Event that triggers when the application is forced ended using task manager [duplicate]

This question already has answers here:
How to do something before process get killed in windows
(3 answers)
Closed 5 years ago.
Is there anything I can do with my program wherein I have to do something when my application has been terminated using task manager?
I hope you can help me in this matter.
Thank you!
You canĀ“t do this because the applications process is forcefully closed (killed). Events only happen if the application is asked to end e.g. call Close() or Exit().
If you need to do something when this happens implement a different app that monitors your app to take actions e.g. restart app or close another app.
I have a windows application that shells an outside application (.exe). What i want to achieve, is when the user terminates my windows application thru task manager, the shelled outside application should also be terminated.
As pointed out by others, you simply cannot do this, because when a process is killed, it receives no events at all. What you can do, however, if this requirement is super important, that you deploy a second program, which is a windows service, that monitors your windows app and shuts down the shelled application whenever it finds that your app is not running any more.

Calling appropriate Exit method for current application from library?

I created a class library to AutoUpdate applications. It needs to kill the application when it finishes downloading the update. Currently, I determine the exit method to use by using below code.
if (Application.MessageLoop)
{
Application.Exit();
}
else
{
Environment.Exit(0);
}
I know that Environment.Exit(0) is a brutal way to kill the application and It doesn't kill the application when the application uses CefSharp. Is there any way I can call Application.Current.Shutdown() when the application is WPF?
If you didn't understand the question you can look at the issue and complete project using below URL.
https://github.com/ravibpatel/AutoUpdater.NET/issues/11
Is there any way I can call Application.Current.Shutdown() when the application is WPF?
Just check if there is any Application object available at runtime:
if (System.Windows.Application.Current != null) //if WPF
System.Windows.Application.Current.Shutdown();
If Application.Current returns an Application object, you can assume that your code executes in the context of a WPF application and you could safely call the Shutdown method.
You should be able to just put in this.close(); in the WPF class

how to automatically exit the application in c# when computer shutdowns? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# Sharp Windows Application prevents Windows from shutting down / logging off
I want to write code for automatically exit from my application when user shutdowns the computer. I have done the project in C# developer.
Now I can't shutdown the computer without manually exiting the application.
Please advice.
You may use SystemEvents class described in below link :-
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.aspx
Subscribe to SessionEnded event which triggers when User logs off or shut down system.
you may write a code in this event to terminate the application by itself.
for eg. Application.Exit() closes the application.
You can use the SystemEvents. Just subscribe to SessionEnded and perform any closing operation you want for your application.
Word of Caution : I think you wont get enough time there to perform any complex task. But since you just want to exit from your application, just call Application.Exit() in the event handler.

WPF form in a console app

Im running a console app that loads a dll and calls a method from that dll that creates a WPF form. So I'm just calling to Program.Execute() method and it does all the creation of the form. All reflection business goes well, but the form does not appear.
I've been told that this is because a console app does not have a windows message loop, but I'm sure there is a way to simulate that. For example, I tried playing with System.Windows.Threading.DispatcherFrame, but to no avail - the form still does not show up.
Did anyone else face this problem?
Just call WPF's Application.Run(). Or Window.ShowDialog(), same thing. You will also have to apply the [STAThread] attribute on your Main() method.

Intercepting application close in c#

I'd like to properly close some sockets, and be able to tell the server that we're closing, but if the application is closed then the sockets are all just closed and on occasion files are left locked. How can I run a method when my application is closed?
This is going to go into a library that will be used in a forms app.
If its a Windows Forms application you can give focus to the form, click the events lightning bolt in the properties window, and use the Form Closing event.
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(OnFormClosing);
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
// Your socket logic here
}
Also if you need to intercept the form being closed set the Cancel property to true:
e.Cancel = true;
I would implement IDisposable on the classes that use these resources. Dispose can then be called explicitly from whichever shutdown method the application type supports (OnFormClose, OnStop, Application_End, ...).
Are you talking about a WinForms app? If so, use the FormClosing event on your main form.
I'm not sure if it's the best place for it, but you can tell your process is closing by hooking AppDomain.ProcessExit. It's fine for cleanup, but I don't think it's a good time to be sending any messages out. :)

Categories

Resources