Closing console closes GUI app too - c#

I'm using C#/WPF, and generated a console programmatically using the WinAPI's Alloc/FreeConsole. When this is done, the standard System.Console class interacts wih it fine. Except, when you close the console by clicking the X rather than using FreeConsole(), it closes the rest of the app too. I'm using .NET 4- how can I override this behaviour?

This is just a guess, and it could be completely off, but does it have anything to do with Application.Current.ShutdownMode? Perhaps Application.Current.MainWindow is being set to the console, and ShutdownMode causes the application to terminate when the console window is closed?

Related

Call a console application without waiting for it

I'm developing a Delphi application which calls a .net console application in hide mode, but the problem is: when I close my delphi application then the console application also closes, even using ShellExecute without specifying to wait for a SingleObject.
I tried that same Shell call for Windows Calc, so now even when I close my app the Calc remains opened and that is the behavior that I'm looking for.
Does someone know if it's possible to call a console application and leave it independent from parent process and how to do that, so it does not close when main application closes?
The issue is that the child process attaches to the console of the parent process. When the parent closes, it's console closes, taking the child with it.
Resolve this by giving the child process its own console. Use CreateProcess passing the CREATE_NEW_CONSOLE process creation flag. You will also want to pass CREATE_NO_WINDOW to avoid showing the new console window.

Windows form application not closing properly

I'm running a simple .NET Windows Forms application. When I click the close button, the Windows form gets closed, but the process is not closing. When I look at the Task Manager, I see that the process for the application is still live, as a background process.
Can somebody explain why that is? Am I missing some function when it's closing?
You need to call
Application.Exit();
in your Form's closing event.
Application.Exit
Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. This is the code to use if you are have called Application.Run (WinForms applications), this method stops all running message loops on all threads and closes all windows of the application.
// WinForms app
System.Windows.Forms.Application.Exit();
Put this code on you from's closing event.
The main reason for not close properly, When your application works with the multi threading. You should use the following, it's works for me as well.
Application.ExitThread();

shutdown wpf while messagebox open

I try to automatically shut down an wpf application at midnight with:
Dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
This works very well in general, but when there is a message box opening waiting for user response, the application fails to shutdown. Is there a way to shut down the application regardless of the opening messagebox?
For WPF Applications use
Application.Current.Shutdown();
Maybe you can use Environment.Exit (immediately exits...very naughty to do on a GUI app) or find the MessageBox window and send them a close message, or hook the creation of any native MessageBox Dialogs (...i.e. track the Window handle, so you can then close them).
Close C# Console Application after MessageBox click
Force to close MessageBox programmatically
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d3f89686-e4d0-4bb1-9052-31abef2a9d2a/
Closing Applications
Right way to close WPF GUI application: GetCurrentProcess().Kill(), Environment.Exit(0) or this.Shutdown()
http://msdn.microsoft.com/en-us/magazine/cc188920.aspx
And a very very naughty way:
Process.GetCurrentProcess().Kill()

How to keep process running after closing the program?

I am now developing an application on Windows Mobile 6.5 with .Net Compact Framework 3.5 using C#. There is a function in the program that I use it to update the location information periodically from server side, but if I keep running this computation, it would cost too much energe. For this reason, I want to run it in background and I try to use BackgroundWorker to do this and it works well.
The problem I have now is that I can't minimize the program so that I have to keep the main form of the program run in foreground even if it's not doing anything and this is very inconvinence for a user. However, when I close the program, the BackgroundWorker will also be closed.
Is there any method to keep the update process running (somewhere in memory or so) when I close the program? and then can restore the information when I restart the program?
How about creating a Service instead of a background worker?
If your Form closes, then Application.Run (probably called over in Program.Main) returns and the process' primary thread exits, causing the application to terminate.
The solution, then, is don't close the Form, simply Hide it. By default the "MinimizeBox" property for your Form should have been true and it should have an [X] in the upper right corner. Clicking this minimizes the Form and will not exit your application.
The other option in some cases is to not have a Form at all. The challenge here is that the CF doesn't have any Application.Run overload that doesn't accept in a Form (like the desktop framework does). The Smart Device Framework does provide one if you want to go that route.
I have not used the .NETCF 3.5. However in the previous version on .NETCF 1.0/2.0 I observed that even if you close the application using (X) button, it just goes to background but remain in the memory.
If that is the case with .NETCF 3.5 as well then I think you do not need to anything here. The background worked will be running even if you close the application.
I Hope this will help you.

Windows Mobile App will not run more than once

I'm having problems getting an application I wrote for Windows Mobile 6.0 to run more than one time on my smart phone.
I built it into a .cab and it installs and runs fine the first time, but if I close the application and try to start it again, it will not run unless I restart the phone.
I have checked the task manager after closing the application and it does not show up.
Are you sure that you are closing your application properly? I recommend using a process manager to see if your application still runs on the background.
My problem was in not handling closing the main Form when the application exited from one of the other forms. Ex: application starts with calculator loaded, user loads unit conversion form from calculator form, and the app hides the calculator. User then closes the unit form and calculator is still running.
All that I missed was handling an OnClosed event in my main form for when the user tried closing the application from one of the other forms.

Categories