multithreading - C# Application not exiting on Shutdown (sometimes) -


my application prevents windows shutting down, on computers, , not time. little tricky debug. think due tcp server. asynchronous server, , application handles closereason == windowsshutdown. when occurs, application still running process, not accessible taskbar/system tray.

i wondering if can see obvious issues server code.

below code server. stop() method called main forms close() event.

public class mantraserver     {         protected int portnumber;         private bool shuttingdown = false;          //the main socket server listens         socket listener;          //constructor - start server on given ip/port         public mantraserver(int port, ipaddress ip)         {             this.portnumber = port;             start(ip);         }          ///          /// description: start threads listen port , process         /// messages.         ///         public void start(ipaddress ip)         {             try             {                 //we using tcp sockets                 listener = new socket(addressfamily.internetwork,                                           sockettype.stream,                                           protocoltype.tcp);                  //assign ip of machine , listen on port number 3000                 ipendpoint ipendpoint = new ipendpoint(ip, 3000);                  //bind , listen on given address                 listener.bind(ipendpoint);                 listener.listen(10);                  //accept incoming clients                 listener.beginaccept(new asynccallback(onaccept), listener);             }             catch (exception ex)             {                 messagebox.show(ex.message, "mantra network start error",                     messageboxbuttons.ok, messageboxicon.error);             }         }          /// decription: stop threads port listener.         public bool stop()         {             try             {                 shuttingdown = true;                 listener.shutdown(socketshutdown.both);                 listener.close();                 listener = null;                 system.threading.thread.sleep(500); //wait half second while server closes                 return true;             }             catch (exception)             {                 return false;             }         }          ///          /// decription: call method accept new connections.         /// <param name="ar">status of asynchronous operation.</param>         private void onaccept(iasyncresult ar)         {             try             {                 if (!shuttingdown)                 {                     mantrastatusmessage inmsg = new mantrastatusmessage();                     inmsg.socket = ((socket)ar.asyncstate).endaccept(ar);                     //start listening more clients                     listener.beginaccept(new asynccallback(onaccept), listener);                      //once client connects start receiving commands them                     inmsg.socket.beginreceive(inmsg.buffer, 0, inmsg.buffer.length, socketflags.none,                         new asynccallback(onreceive), inmsg);                 }             }             catch (exception ex)             {                 messagebox.show(ex.message, "mantra network accept error",                     messageboxbuttons.ok, messageboxicon.error);             }         }          ///           /// receives data, puts in buffer , checks if need receive again.           public void onreceive(iasyncresult result)         {             mantrastatusmessage inmsg = (mantrastatusmessage)result.asyncstate;             int read = inmsg.socket.endreceive(result);             if (read > 0)             {                 (int = 0; < read; i++)                 {                     inmsg.transmissionbuffer.add(inmsg.buffer[i]);                 }                 //we need read again if true                   if (read == inmsg.buffer.length)                 {                     inmsg.socket.beginreceive(inmsg.buffer, 0, inmsg.buffer.length, socketflags.none, onreceive, inmsg);                     console.out.writeline("message big!");                 }                 else                 {                     done(inmsg);                 }             }             else             {                 done(inmsg);             }         }          ///           /// deserializes , outputs received object           public void done(mantrastatusmessage inmsg)         {             console.out.writeline("received: " + inmsg.msg);             mantrastatusmessage received = inmsg.deserialize();             console.writeline(received.msg.message);         }     } 

edit

thanks hogan, more information on call close():

a request send or receive data disallowed because socket not connected , (when sending on datagram socket using sendto call) no address supplied.

not entirely sure means yet.

you have add logging windows event log see going on.

the best place start in catch returns false (since stop windows shutting down.) if log reason there @ least can @ event log see why service won't shut down.


Comments