Alert user about the running process during installation - c#

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.
}

Related

How to not open multiple windows at the same time for CAM interface

I'm writing plugin/interface for CAM/CAD software and I use this code to open a "SaveWindow".
public void Run(string theMode)
{
try
{
if (theMode == "SaveWindow")
{
string aPictureString = GetPictureString();
StartInterface(null, theMode, CreateAndSaveTheToolList(aPictureString));
}
else
{
string aPipeId = GetRandomString();
itsServerStream = new NamedPipeServerStream(aPipeId, PipeDirection.In, 1);
ThreadPool.QueueUserWorkItem(this.ListenToStream);
StartInterface(aPipeId, theMode, "");
StartNamedPipe(aPipeId);
itsRefreshThread = new Thread(this.RefreshTools);
itsRefreshThread.Start();
if (!InitLogger(Path.GetDirectoryName(this.GetType().Assembly.Location)))
{
MessageBox.Show(//Secured code);
return;
}
}
itsLogger.Info("Run execute was successful.");
}
catch (Exception aException)
{
//Secured code
}
LogManager.ResetConfiguration();
}
If there is an interface open and I click the plugin button again it opens another multiple one. How do I code to not to open the second one if the first is open.
Your current situation is something like
if (condition)
{
// Open your window
}
else
{
// Do something else
}
So every time you satisfy your condition another instance of your window is opened.
You can get around this problem by checking whether your window is already open like this
bool isOpen = false;
if (!isOpen)
{
// The window isn't open so open it
isOpen = true;
}
else
{
// The window is already open so don't open it again
}
In this case the question is in the situations when your condition is satisfied but your window is open what would you like to do?
Simply adding the isOpen check to your open window path like this
if (condition && !isOpen)
{
// Open your window
isOpen = true;
}
else
{
// Do something else
}
Will mean that whenever your condition is satisfied and the window is already open your code will "Do something else".
An alternative approach is something like this
if (condition)
{
if (!isOpen)
{
// Open your window
isOpen = true;
}
else
{
// Do something else
}
}
else
{
// Do something else
}
This means that when your condition is satisfied you open the window if it isn't open and do something else if it is. Then your third case is the second "Do something else" path for when the condition isn't satisfied.

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()

Boolean Resets When App Loads Wp8

Hi I am still fairly new to C# & windows phone.
When the app Loads I wanted popup asking the user if they would like to do something
MessageBoxResult m = MessageBox.Show("Info.", "Question?", MessageBoxButton.OKCancel);
if (m == MessageBoxResult.Cancel)
{ }
else if (m == MessageBoxResult.OK)
{ //Do Something }
Now that works fine, if the user says no I wanted a popup that asked the user if they would like reminding next time so U used
MessageBoxResult m = MessageBox.Show("Info.", "Question?", MessageBoxButton.OKCancel);
if (m == MessageBoxResult.Cancel)
{
MessageBoxResult r = MessageBox.Show("", "Would You Like Reminding Next Time ?",MessageBoxButton.OKCancel);
if (r == MessageBoxResult.Cancel)
{ }
else if (r == MessageBoxResult.OK)
{ }
}
else if (m == MessageBoxResult.OK)
{ //Do Something }
I need some kind of a switch, so when the app starts for the first time
app checks switch which is on,
they get asked a question
if they answer cancel,
they get asked if they want reminding
if they answer no,
set switch to off
I've tried to use a boolean but it just resets to true when the app closes, if i use a string it says a string cant be used as a bool
Any Advice ?
Use IsolatedStorageSettings.ApplicationSettings to quickly save small values for example
// this will save my "your_key" to false;
IsolatedStorageSettings.ApplicationSettings.Add("your_key", false);
IsolatedStorageSettings.ApplicationSettings.Save(); // make sure you call save
// so the next time the app runs I can get it back doing this
bool your_key = (bool) IsolatedStorageSettings.ApplicationSettings["your_key"];
But, should always enclose it in a try catch because the key might not exist
bool your_key = false; // or default value
try
{
your_key = (bool) IsolatedStorageSettings.ApplicationSettings["your_key"];
}
catch(Exception ex)
{
}
More Information can be found here:
How to: Store and Retrieve Application Settings Using Isolated Storage
if(!IsolatedStorageSettings.ApplicationSettings.Contains("first"))
{
// Do your stuff
IsolatedStorageSettings.ApplicationSettings["first"] = true;
IsolatedStorageSettings.ApplicationSettings.Save();
}
This is all the code you need.
Place everything you want to do only on first launch into this if statement. Then execute this code either in main page Loaded event or OnNavigatedTo.

WPF window close is not working in one click

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()

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