Open dialog box prevents windows shutdown - c#

I have a C# GUI app that shows a message using MessageBox.Show(Message);, however if the user fails to click on this, and then requests shutting down the PC, it blocks the shutdown.
How do I prevent my open dialog box from blocking the shutdown?

I'm assuming you're using WinForms since you didn't mention WPF. You can't use a MessageBox if you want to control closing behavior. You'll have to build your own screen to act as a message box and use the ShowDialog method to display it. Your screen can handle the FormClosing event to detect when Windows is shutting down:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown)
{
//...
}
}
So you'll want to allow the screen to close in this case and perhaps take other action for other types of close signals. To prevent the screen from closing, set the Cancel flag on the FormClosingEventArgs parameter to true;

Related

Show a Window while application is closing

I've a very heavy application that takes some time to close itself (it also depends on the pc where it is running.. but this is not the matter right now)
I would like to show a custom window with a message during this closing time, but as soon as i call the "Shutdown" method every window disappears (except made for the MessageBox).
This is the code i'm trying to use to achieve my objective
void MainWindow_Closing(object sender, CancelEventArgs e)
{
Task.Factory.StartNew(() =>
{
var closingWaitTest = "application closing, please wait;
Application.Current.Dispatcher.Invoke(() =>
{
var closingSplash = new ClosingSplashWindow(closingWaitTest);
closingSplash.Show();
});
MessageBox.Show(closingWaitTest);
});
Application.Current.Shutdown();
}
I Added a messageBox just to check, and it actually works. I mean, the MessageBox stays open until the application process is alive (i check that from the windows TaskManager) while my Window is instantly closed.
Hope someone can give some advice about this,
thanks in advance (:
EDIT -
So, the main problem is that as soon as i call the Application.Current.Shutdown my splash window instantly closes, while the application process is still up and running for some time (disposing all my things before calling shutdown actually reduced this time a bit).
The point is that i would like to show a window for the entirety of time that the process is still up; given the fact that a MessageBox behaves exactly like that, my question is:
Is there a way to make my ClosingSplashWindow behave like a MessageBox and stay visible until the application process is really dead?
Since Application.Current.Shutdown(); is going to close the application immediately. Maybe you first have a flag to track that application is being closed, cancel the Closing event and initiate the Resource cleanup followed by Application.Current.Shutdown(); again.
The Application_Closing handler may get fired once again, since you've a flag which says you're about to close you can directly exit the handler and all should be good.
First of all, you want to have a flag which indicates that your application is currently shutting down:
private bool IsShuttingDown { get; set; }
Then you should cancel closing operation, perform some heavy work and shut down your application:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!IsShuttingDown)
{
e.Cancel = true;
ShowSplashWindow();
PerformHeavyOperation();
Application.Current.Shutdown();
IsShuttingDown = true;
}
}
In the end I solved the problem creating another application that shows a window while the main application process is still up and running.
I know it is not the best way to do it, but i was not able to do it in any other way..
Thank for the support! :)

Close winform but keep running in task bar

Silly thing I need help with.
When I run my application if I hit the X button to close the window, it stops the application. What I want is for it to hide the winform but keep running on the task bar (already have it running on the task bar).
Is there a simple solution for me to do this?
Keeping the taskbar button requires the window to stay alive. You can do so by just having the window minimize instead of close. Which requires implementing the FormClosing event, something like this:
bool shouldTerminate;
protected override void OnFormClosing(FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing && !shouldTerminate) {
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
base.OnFormClosing(e);
}
You can handle formclosing event and make it hide the form instead of closing it.
void myForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
Rather than keep it running in Taskbar, what you need to do is to hide it on application load, and keep it running in System Tray area. The NotifyIcon control will help you achieve this behavior. You can find how to implement such behavior in this MSDN article.
Also see:
Creating Tray Applications in .NET: A Practical Guide
minimize app to system tray
How can I make a .NET Windows Forms application that only runs in the System Tray?

How to prevent the application from closing when clicking the close button in a C# windows app?

I am coding a Windows form based application in C#.
When I click on the red cross(x) on the top right corner of the windows it should not stop the execution of the program. It should instead go to next part of the program. What do I need to do in order to have my program behave this way?
You can use the form closing event to cancel the closing of the form. In the designer click the lighting symbol in the properties and double click the form closing entry. This should make the code for you. It should make a function a bit like this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
You need to add the e.Cancel that will stop the form closing

Changing Wpf window appearance on Window_Closing

When the user clicks the x to close the application window I want to pop up a message saying "Shutting Down, please wait" and greyout or disable the rest of the window controls.
This is because my application shutdown and cleanup takes a little while, about 20 seconds.
However, wpf commands in the Window_Closing event handler never seem to do anything. I have a tranparent grey-filled border which overlays the window controls and I want to change the visibility from collapsed to visible. The code is called but nothing changes.
It is as if the request is queued but is not called because the window is closing.
Is there some way to flush this request out?
Edit:
Try this code:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
_DisableBorder.Visibility = System.Windows.Visibility.Visible;
Thread.Sleep(2000);
}
The app will close after 2 sec, but the border will never show. If you add e.Cancel = true, then the border will show after 2 sec.
You should make a trick.
Within the "Closing" handler, you should cancel the closing, then show the popup (as modeless window), then disable everything on the main window. When the process is over, you can close both the popup and the main windows programmatically.
I'd not perform anything time-consuming within the closing handler.
Hope it helps.
Cheers
Calling DoEvents from System.Windows.Forms.Application seems to do the trick..
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
_DisableBorder.Visibility = System.Windows.Visibility.Visible;
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(2000);
}

c# WinForms form still closes after setting e.Cancel = true in Form_FormClosing event

This is the code in question:
private void FormAccounting_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.FormAccountingLocation = this.Location;
Properties.Settings.Default.Save();
if (IsEditing)
{
MessageBox.Show("Please save or cancel open transactions before closing the accounting window.", "Open Transactions", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true;
}
}
I've added breakpoints to the e.Cancel = true; line to ensure it's being executed.
The form closes immediately after clicking Ok.
Here's the code that calls FormAccounting:
private void buttonAccounts_Click(object sender, EventArgs e)
{
FormAccounting NewFormAccounting = new FormAccounting();
NewFormAccounting.Show();
}
Canceling the form close event works to prevent:
User closing the form
Application.Exit from exiting the application
Code from calling Form.Close on the form
But it does not work to prevent:
User closing application's main form
Code calling Form.Dispose on the form
Code calling Form.Close on the application's main window
The last 3 cases don't even trigger the form close event on the non-main form, so the form goes away without a chance to cancel it. Perhaps your application is causing the form to first close in one of the first 3 ways, which triggers the event, and then in one of the second 3 ways (or something similar), which does not trigger the event and forces the form closed anyway.
Edit:
Add this function to your form's code and it will allow you to review in the debugger what the call stack looks like when your window is getting closed so you can see what is actually causing it:
protected override void DestroyHandle()
{
System.Diagnostics.Debugger.Break();
base.DestroyHandle();
}

Categories

Resources