Process exit on closing WPF application [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
A WPF appplication is hosted using
System.Diagonistics.Process
I have subscribed to the exit event of the process. I wanted to know how the window's close triggers the exit event.

The process exits when the call to Application.Run() in the Main method returns.
Application.Run() in turns calls Dispatcher.Run() which keeps the dispatcher loop running until the framework calls App.CriticalShutdown, assuming the App.ShutdownMode is set to either ShutdownMode.OnLastWindowClose or ShutdownMode.OnMainWindowClose. Otherwise you shut down explicitly.
If you look in the source code, you'll see an example of how App.CriticalShutdown is called from the Window class:
if (((App.Windows.Count == 0) && (App.ShutdownMode == ShutdownMode.OnLastWindowClose))
|| ((App.MainWindow == this) && (App.ShutdownMode == ShutdownMode.OnMainWindowClose)))
{
App.CriticalShutdown(0);
}

Related

How to close program if , if else ,equals no [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I just started C#.
I can't seem to find a way of the program closing it if the, if else, equals no.
I've tried :
-> System.Environment.Exit(1);
-> public static void Exit ();
A part of the Code:
Console.WriteLine("Jes/No");
string input = Console.ReadLine();
if (input == "Jes")
{
Console.WriteLine("Welcome to the Apex Libary");
}
if (input == "No")
{
________________________
}
The Blanks :______________ , are a space holder so that I know where to put the code.
Use :
Environment.Exit(0);
This command is used to close the application by providing an exit code.

Detect when none of app windows is active/focused [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How detect when user changes focus from multi-window application (alt+tab e.g.).
I want to detect when none of the app windows is active/focused.
First window is always shown but user can work with up to four windows(none of these is shown as dialog).
Form has a ContainsFocus property that indicates whether the form, or one of its child controls has the input focus. You can check this property for all open forms to detect if the application contains focus or not:
var isActive = Application.OpenForms.Cast<Form>().Any(x=>x.ContainsFocus);
Also as another option:
var isActive = (Form.ActiveForm != null)
If you want to be notified of the state of the application you can handle Activate and Deactivate event of your forms for all forms.
private void f_Deactivate(object sender, EventArgs e)
{
BeginInvoke(new Action(() =>
{
if (Form.ActiveForm == null)
Text = "App Deactivated.";
else
Text = "Still Active";
}));
}

Validating data structures after thread termination/exit [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have multiple threads in my program, one of which is operating on an internal data structure. Due to some error, this thread quits leaving the data structure in an invalid state. How can other threads properly validate the state of data structure on later access? In general, how to handle such a scenario?
The best answer is to make sure that threads don't quit leaving the data structure invalid. Other than that, the only solution is something like:
In class:
bool m_data_valid = true; // Or possibly 'false' and set it true in constructor
In mutating thread:
m_data_valid = false;
... // Mutate structure
m_data_valid = true;
In other threads:
if (!m_data_valid)
fixup(); // Or whatever you were going to do.

call a console application [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the code to call a console application within a WPF? I have an application in WPF you need:
call a console application;
the console application closes the WPF
The console application again calls WPF and close console application.
This is necessary because I am doing a system where updates to the application must close the files to copy.
For close application WPF I am using the following:
Process wpfProc = Process.GetProcessesByName("MainWindow.exe").First();
wpfProc.Kill();
return in console application:
Unhandled Exception: System.InvalidOperationException: Sequence sontains no elements
at System.Link.Enumerable.First[TSource]<IEnumerable'1 source>
at Updater.Program.Main<String[] args> in d:\endereçodoUpdater\Program.cs:line 17
in line 17 have the following:
Process wpfProc = Process.GetProcessesByName("MainWindow").First();
How do I resolve this?
You do this with Process.Start:
Process myProc = Process.Start("MyConsoleApp.exe");
//Close gracefully
Application.Exit();
In MyConsoleApp.exe, you would need to use GetProcessByName to kill your WPF app, and then Process.Start again to restart it:
Process wpfProc = Process.GetProcessesByName("MyWpfApp.exe").First();
//If you want to directly kill it
wpfProc.Kill();
//Or be nice and let it kill itself
wpfProc.WaitForExit();
//Do stuff
Process.Start("MyWpfApp.exe");
System.Diagnostics.Process on MSDN

How to throttle C# workload? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm working with a file watcher. It only needs to perform the action after no more events have fires for like 10 seconds.
For these types of problems I use a throttle function in JavaScript and I was wondering if C# can do something similar:
var t = null;
function throttleAction(fn){
if(t != null){
window.clearTimeout(t);
}
t = window.setTimeout(function(){
t = null;
fn();
}, 10000);
}
How would I implement something like this in C#?
Use System.Timers.Timer. Set the Interval property to your chosen interval, write your worker function and then pass it as a delegate to the Elapsed event. Finally, Start the timer.

Categories