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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -