none
Running a Console application as a Windows service RRS feed

  • Question

  • Hi Folks,
    I'm building a service application using a librabry named Quick FIX. The library has a .start() fucntion that will listlten to the client requests(TCP/IP). In an console application I need to have  a console.readln() for the above start() method to listen to clients and in I need to do this in a service application.  (using .net 2.0 c#)

    since the start() close the port if I don't include a readln() after the start()  I can't get the windows service to run continuousely. The service starts and stop listening to clients after few seconds.

    Can any one suggest a way to get around this.

    Many thanks,
    Vijay
    Friday, May 9, 2008 3:47 PM

Answers

  • What makes it stop listening?  Without Console.ReadLine(), your console app will terminate.  No doubt, that makes it stop listening.  In a service, you'd start a thread in the OnStart() method.  That thread should call start(), then WaitHandle.WaitOne() on a wait handle that signals that it should stop (signalled by OnStop).
    Saturday, May 10, 2008 1:24 PM
    Moderator
  • Note the "start a thread on OnStart()" bit in my previous post.
    Monday, May 12, 2008 11:19 AM
    Moderator

All replies

  • What makes it stop listening?  Without Console.ReadLine(), your console app will terminate.  No doubt, that makes it stop listening.  In a service, you'd start a thread in the OnStart() method.  That thread should call start(), then WaitHandle.WaitOne() on a wait handle that signals that it should stop (signalled by OnStop).
    Saturday, May 10, 2008 1:24 PM
    Moderator
  • Thanks For the post. I tried this and as follows.

    static AutoResetEvent autoEvent = new AutoResetEvent(false);
      protected override void OnStart(string[] args)
            {
                // TODO: Add code here to start your service.
               // eventLog1.WriteEntry("VicUK QFIX Starting");
                try
                {
                    string ConfigPath = ConfigurationManager.AppSettings["QFIXSettingsFilePath"];
                    //SessionSettings settings = new SessionSettings(args[0]);
                    SessionSettings settings = new SessionSettings(ConfigPath);
                    //      SessionSettings settings = new SessionSettings(@"C:\TEMP\Initiator\Testing.settings");
                    Application application = new Application();
                    FileStoreFactory storeFactory = new FileStoreFactory(settings);
                    ScreenLogFactory logFactory = new ScreenLogFactory(settings);
                    MessageFactory messageFactory = new DefaultMessageFactory();
                    SocketInitiator initiator
                    = new SocketInitiator(application, storeFactory, settings, logFactory, messageFactory);
                    initiator.start();
                 autoEvent.WaitOne();
                
                  }
                catch (Exception e)
                {
                    eventLog1.WriteEntry(e.Message.ToString(), EventLogEntryType.Error);
                }
            }

    This  will  make the thread running but the service hangs when it's  trying to start.  Am I missing something  ?
      Please advice,
    Thanks,
    Vij
    Monday, May 12, 2008 11:09 AM
  • Note the "start a thread on OnStart()" bit in my previous post.
    Monday, May 12, 2008 11:19 AM
    Moderator
  • Hi,
    Thanks.  Once I have put the .stop() in the onstop() the service starts working like a charm. Thanks v much.
    -Vij
    Monday, May 12, 2008 3:04 PM