windows service

Dec 6 2004 12:18 AM
hai i wrote a windows service program with multithreading.but the service would not start.it take some delay to perform the start operation. here is the code ////////////////////////////code starts///////////////////////////////////////////////// using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; namespace WinService { using System.IO; using System.Net.Sockets; using System.Text; using System.Threading; public class SynchronousSocketListener { private const int portNum = 1234 ; private static ArrayList ClientSockets ; private static bool ContinueReclaim = true; private static Thread ThreadReclaim ; public static void StartListening() { ClientSockets = new ArrayList() ; ThreadReclaim = new Thread( new ThreadStart(Reclaim) ); ThreadReclaim.Start() ; TcpListener listener = new TcpListener(portNum); try { listener.Start(); int TestingCycle = 3 ; int ClientNbr = 0 ; // Start listening for connections. Console.WriteLine("Waiting for a connection..."); while ( TestingCycle > 0 ) { TcpClient handler = listener.AcceptTcpClient(); if ( handler != null) { Console.WriteLine("Client#{0} accepted!", ++ClientNbr) ; // An incoming connection needs to be processed. lock( ClientSockets.SyncRoot ) { int i = ClientSockets.Add( new ClientHandler(handler) ) ; ((ClientHandler) ClientSockets[i]).Start() ; } --TestingCycle ; } else break; } listener.Stop(); ContinueReclaim = false ; ThreadReclaim.Join() ; foreach ( Object Client in ClientSockets ) { ( (ClientHandler) Client ).Stop() ; } } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); } private static void Reclaim() { while (ContinueReclaim) { lock( ClientSockets.SyncRoot ) { for ( int x = ClientSockets.Count-1 ; x >= 0 ; x-- ) { Object Client = ClientSockets[x] ; if ( !( ( ClientHandler ) Client ).Alive ) { ClientSockets.Remove( Client ) ; Console.WriteLine("A client left") ; } } } Thread.Sleep(200) ; } } } class ClientHandler { TcpClient ClientSocket ; bool ContinueProcess = false ; Thread ClientThread ; public ClientHandler (TcpClient ClientSocket) { this.ClientSocket = ClientSocket ; } public void Start() { ContinueProcess = true ; ClientThread = new Thread ( new ThreadStart(Process) ) ; ClientThread.Start() ; } private void Process() { // Incoming data from the client. string data = null; // Data buffer for incoming data. byte[] bytes; if ( ClientSocket != null ) { NetworkStream networkStream = ClientSocket.GetStream(); ClientSocket.ReceiveTimeout = 100 ; // 1000 miliseconds while ( ContinueProcess ) { bytes = new byte[ClientSocket.ReceiveBufferSize]; try { int BytesRead = networkStream.Read(bytes, 0, (int) ClientSocket.ReceiveBufferSize); if ( BytesRead > 0 ) { data = Encoding.ASCII.GetString(bytes, 0, BytesRead); // Show the data on the console. Console.WriteLine( "Text received : {0}", data); // Echo the data back to the client. byte[] sendBytes = Encoding.ASCII.GetBytes(data); networkStream.Write(sendBytes, 0, sendBytes.Length); if ( data == "quit" ) break ; } } catch ( IOException ) { } // Timeout catch ( SocketException ) { Console.WriteLine( "Conection is broken!"); break ; } Thread.Sleep(200) ; } // while ( ContinueProcess ) networkStream.Close() ; ClientSocket.Close(); } } // Process() public void Stop() { ContinueProcess = false ; if ( ClientThread != null && ClientThread.IsAlive ) ClientThread.Join() ; } public bool Alive { get { return ( ClientThread != null && ClientThread.IsAlive ); } } } // class ClientHandler public class Service1 : System.ServiceProcess.ServiceBase { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Service1() { // This call is required by the Windows.Forms Component Designer. InitializeComponent(); // TODO: Add any initialization after the InitComponent call } // The main entry point for the process static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun; // More than one user Service may run within the same process. To add // another service to this process, change the following line to // create a second service object. For example, // // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()}; // ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { components = new System.ComponentModel.Container(); this.ServiceName = "Service1"; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } /// /// Set things in motion so your service can do its work. /// protected override void OnStart(string[] args) { // TODO: Add code here to start your service. base.OnStart(args); SynchronousSocketListener.StartListening(); } /// /// Stop this service. /// protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. base.OnStop(); } } } /////code end//////////////////////////////////////////////////////////////////// plz inform wht is the problem of this program with regards lisha