c# - Exit a windows console application to clean up resources -
i have console application, in have connection third party windows service on remote server tcp/ip.
the call hierarchy likes:
static class program { [mtathread] static void main() { myapplication.start();
the start
method
public static void start() { lock (syncvar) { threadstart ts = new threadstart(maincode); maincodethread = new thread(ts); maincodethread.isbackground = false; maincodethread.start();
the detail of main thread has:
private static void maincode() { try { // connect remote server, against windows service telephonyserver tserver = new telephonyserver(sipaddress, "username", "password"); while (true) { task consumer = task.run(() => { if (data != "") { processeachchannel(data); }); task producer = task.run(() => { // blah blah });
in method processeachchannel
, have
public bool processeachchannel(string workitem) { channelresource cr = tserver.getchannel(); // blah blah }
now application working well. if click red exit cross of application or click stop debugging button visual studio, resources channelresource cr
not destroyed @ all. found fact remote server service control dashboard.
i tried code
system.diagnostics.process process = system.diagnostics.process.getcurrentprocess(); process.exited += new eventhandler(onexited);
it not helpful. heard tricks manage thread passing parameters main thread set true
or false
etc no clue.
if kill process, instead of closing gracefully (and when close console window or press stop buttpn in debugger), there's no chance cleanup code run. have implement exit handler, perhaps catching ctrl-c press , return threads, objects can cleanly dispose themselves.
Comments
Post a Comment