c# - how correctly to end all process of program -
my program has event onclosing - hide it. need implement closing of programm - try use context menu of task bar notification item.
code:
private void formmainform_formclosing(object sender, formclosingeventargs e) { //canceling closing form e.cancel = true; //hide form this.windowstate = formwindowstate.minimized; } private void toolstripmenuitem1_click(object sender, eventargs e) { //hide icon tray notifyicon.visible = false; //get current process process proc = process.getcurrentprocess(); //kill , close programm proc.kill(); } but read little bit, kill() terminate work , "kill" process, think can not normal way close programm - data can destroyed or not stored
also try use proc.closemainwindow(); , proc.close() - no effect on programm - windows closed, process still running.
question: .kill() it's correct way close programm, or ther way it?
you call .close() in menuitem click , set boolean flag onclose handler can check flag , know should really close. lot safer trying kill own process.
private bool onlyhideonclose = true; private void formmainform_formclosing(object sender, formclosingeventargs e) { if(this.onlyhideonclose) { e.cancel = true; this.windowstate = formwindowstate.minimized; } } private void toolstripmenuitem1_click(object sender, eventargs e) { this.onlyhideonclose = false; this.close(); }
Comments
Post a Comment