Saturday 15 May 2010

c# - what is the best practice to exit an WPF application? -



c# - what is the best practice to exit an WPF application? -

i maintaining existing c# application, , noticed next code not working expected.

private void form1_load(object sender, eventargs e){ ... if (proc.length == 0) { proc = process.getprocessesbyname("opcon"); if (proc.length == 0) { writelog("dataloggerservice start: no tss process detected; close;"); this.close(); } } ... }

the code supposed exit after close() api call. however, still proceed.

after reading , research, modified to

private void form1_load(object sender, eventargs e){ .... if (proc.length == 0) { proc = process.getprocessesbyname("opcon"); if (proc.length == 0) { writelog("dataloggerservice start: no tss process detected; close;"); this.dispose(); environment.exit(0); } } .... }

it seems exit expected. however, not confident whether best practice?

is necessary phone call this.close() or this.dispose() before environment.exit()?

thanks.

regards, sqr

in wpf application whenever mainwindow specified startupuri in app.xaml closed application exits automatically.

still if want handle exit of application on end can go below solution.

override onclosing of mainwindow , manually exit/shutdown application.

protected override void onclosing(system.componentmodel.canceleventargs e) { // shutdown application. application.current.shutdown(); // or can go below logic // environment.exit(0); }

c# wpf exit

No comments:

Post a Comment