C# Windows Service shutting down
-
Hi Guys, I need some help please. I have the following code being called in my timer_elapsed function and my timer is being enabled in the onstart of the windows service. This worked fine with .net 1.1 but ever since I have converted the application to .net 2.0, my service shuts down without notice when there is trafic in client requests i.e. ProcessClientRequest call. When I try to restart the service upon shut down it gives me an error saying "Only one usage of each socket address (protocol/network address/port) is normally permitted" So I have to stop and wait a few and restart and it works fine and then again when there is much activity in processclientrequest call it shuts down. I know this is not much information but please help me if you have any ideas. Writ2ErrLog("Service Timer Initialized"); string sErr = "Application is ready for processing"; try { Writ2ErrLog(sErr); m_tMonitor = new TD.Timer(new TD.TimerCallback(MonitorFunc), 1, 0, 45000);//45 sec if (m_sServerAddr == string.Empty) { m_sServerAddr = Dns.GetHostName(); } if (m_nPort == -1) { m_nPort = 1024; } IPHostEntry hostEntry = Dns.GetHostEntry(m_sServerAddr); IPAddress ipAddress = hostEntry.AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, m_nPort); m_svSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_svSocket.Bind(ipEndPoint); m_svSocket.Listen(10); while (true) { try { Socket rtSocket = m_svSocket.Accept(); if (rtSocket.Connected) { TD.WaitCallback myCallBack = new TD.WaitCallback(ProcessClientRequest); TD.ThreadPool.QueueUserWorkItem(myCallBack, rtSocket); } } catch (Exception ex) { Writ2ErrLog(ex.Message); break; } } } catch (Exception ex) { m_svSocket.Close(); m_tMonitor.Dispose(); sErr = "