WPF window close is not working in one click - c#

I am alerting the user about the process complete like :
var result = MessageBox.Show("Completed!!", "Upload Status", MessageBoxButton.OK, MessageBoxImage.Information);
if (result == MessageBoxResult.OK)
{
result = MessageBox.Show("Do you want to Quit the application ?", "Quit Application", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
OpenWebSite();
// I want to close the message box and complete application.
}
}
If I write this.Close..It is giving exception, So I wrote it like this :
Dispatcher.BeginInvoke(new Action(() => { this.Close(); }));
Still the page is only closing after two click on [Yes] button, which is making the the OpenWebSite(); to be called two times. ...
Any idea on what is causing this?

Use Application.Current.Shutdown()

Related

How to create MessageBox in Unity without event/delegate callback?

There is one part in MessageBox that I need to re-create in Unity.
DialogResult result = MessageBox.Show(/*parameters*/);
//When code runs - appears MessageBox and next lines of code WAIT for result
//Only if button is pressed - code proceeds next lines.
if (result == DialogResult.Yes) { DoIt(); }
else { DoNotDoIt(); }
I've seen some realization with Action callbacks.
In that realization - MessageBox has Action fields and in MessageBox.Show() method you should add delegates to propriate Action's as parameters.
I can't use it in my case. I need to ensure that dialog result is set and only then continue to run code.
You can run your code in Update. DialogResult could have 3 states, None, Yes and No. With None, you do nothing, Update returns and comes back next frame:
void Update()
{
DialogResult result = MessageBox.Show(/*parameters*/);
if (result == DialogResult.Yes) { DoIt(); }
else if(result == DialogResult.No) { DoNotDoIt(); }
}
other way is coroutine which is basically same as update:
IEnumerator DialogSequence()
{
while(MessageBox.Show(/*parameters*/)== DialogResult.None) { yield return null;}
DialogResult result = MessageBox.Show(/*parameters*/);
if (result == DialogResult.Yes) { DoIt(); }
else if(result == DialogResult.No) { DoNotDoIt(); }
}

Overriding standart close button in Unity

I want to create a "Are you sure?" system when user clicks the close button of window. Is it possible to catch FormClosing event in Unity ?
Thanks.
Edit:
void OnApplicationQuit()
{
DialogResult result = MessageBox.Show(
"Are you sure you want to cancel ?",
"Question",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Application.Quit();
}
}
I tried this to open a dialog when user clicks (X) button. It works but, looks like it creates a new dialog each frame.
This question needs an updated answer as Application.CancelQuit() is deprecated and MonoBehaviour.OnApplicationQuit() doesn't prevent the application from closing.
To accomplish this exit confirmation method, it's better to use: Application.wantsToQuit.
From the docs:
Unity raises this event when the player application wants to quit.
Add an event handler to this event to receive a notification that application is attempting to quit.
When this event is raised the quit process has started but can be cancelled. This means the player is not guaranteed to quit. For a guaranteed quit event take a look at Application.quitting.
Return true and the quit process will continue. Return false and the quit process will cancel.
Example:
// When the application starts, append a method
// that will run when the user attempts to exit
[RuntimeInitializeOnLoadMethod]
static void RunOnStart() {
Application.wantsToQuit += WantsToQuit;
}
public static bool quitConfirmation = false;
static bool WantsToQuit() {
if(quitConfirmation) {
return true;
} else {
RequestQuitConfirmation();
}
return false;
}
static void RequestQuitConfirmation() {
DialogResult result = MessageBox.Show(
"Are you sure you want to cancel ?",
"Question",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
quitConfirmation = true;
Application.Quit();
}
}
Note: The return value of this event (Application.wantsToQuit) is ignored when exiting play mode in the editor. IMPORTANT: The return has no effect on iPhone. Application can not prevent termination under iPhone OS.
Have a look at MonoBehaviour.OnApplicationQuit() and Application.CancelQuit()

WPF Cross-Thread MessageBox not blocking main thread input

I'm trying to show a MessageBox from another Thread using Dispatcher.Invoke.
The MessageBox did show up. But without clicking the OK button on the MessageBox, I can still control the main window.
I'm thinking if there's a way to block the main window input before clicking the OK button?
Any help is appreciated.
Code I have so far:
void ShowError(String message) //I call this function in another thread
{
if (this.Dispatcher.CheckAccess())
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
else
{
this.Dispatcher.Invoke(
new Action(() =>
{
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}));
}
}
The only way to be guaranteed that the message box blocks the window is by passing the window in to the message box.
void ShowError(String message, Window windowToBlock) //I call this function in another thread
{
if (this.Dispatcher.CheckAccess())
MessageBox.Show(windowToBlock, message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
else
{
this.Dispatcher.Invoke(
new Action(() =>
{
MessageBox.Show(windowToBlock, message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}));
}
}

Alert user about the running process during installation

protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
DialogResult result = DialogResult.None;
if (isExcelIsRunning())
{
result = MessageBox.Show("Excel is still running. Please save your work and close Excel, and then click \"Retry\" button to continue with installation.", "", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
}
while (result == DialogResult.Retry)
{
if (!isExcelIsRunning())
{
break;
}
}
}
//check if excel is currently running
private bool isExcelIsRunning()
{
bool flag = false;
Process[] xlProc = Process.GetProcessesByName("excel");
if (xlProc != null)
flag = true;
return flag;
}
The above is the code I am about to use for my Installer Class.
What I want to achieve here is that I need the installer to check whether or not Excel is currently running during the installation. And if it is running, then there should be a pop-up message at the start of installation to alert user to close off Excel and the installer should pause until there's no Excel instance found in the process list any more.
Back to the code, I don't think that while block is quite right, as it could cause an endless loop after user clicks "Retry" button if in the mean time Excel is still running.
So what'd be the better approach? Would anyone here take a look at the above code and see where I can improve?
I think your code would be something like this:
while(isExcelRunning())
{
var result = MessageBox.Show("Excel is still running. Please save your work and close Excel, and then click \"Retry\" button to continue with installation.", "", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
if (result != DialogResult.Retry)
{
//Handle Cancel
break;
}
}
This will keep displaying the alert in a loop, either until Excel exits, or they press cancel, at which you can do whatever you need to do; then we exit the loop (or you could return entirely out of the method).
The key here is display the alert repetitively, either until Excel is gone or they choose to cancel. If you need to do some code after the loop based on the response; perhaps something like this:
var userHasCancelled = false;
while(!userHasCancelled && isExcelRunning())
{
var result = MessageBox.Show("Excel is still running. Please save your work and close Excel, and then click \"Retry\" button to continue with installation.", "", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
if (result != DialogResult.Retry)
{
userHasCancelled = true;
}
}
if (userHasCancelled)
{
//User cancelled...
}
else
{
//Continue.
}

How restart the Window?

WPF
I need CLEAN and START again the window SetPathCharger.xaml when the user clic on "Yes" the message box, the problem is the application send a error InvalidOperationException.
public void ExitProgram(string message)
{
var restart = MessageBox.Show("Do you want do it again?",
"Question", MessageBoxButton.YesNo,
MessageBoxImage.Question).ToString();
if (restart == "Yes")
{
_setPathCharger.ShowDialog();
}
if (restart == "No")
{
Environment.Exit(0);
}
}
How can I do this?
You should just create and show a new SetPathCharger window, instead of reusing the current one. Something like:
_setPathCharger = new SetPathCharger();
_setPathCharger.ShowDialog();
Assuming ExitProgram is in some outer scope and is triggered after closing _setPathCharger then I suppose you are trying to ShowDialog() a disposed object.
Try to:
_setPathCharger = new SetPathCharger();
_setPathCharger.ShowDialog();

Categories