0

So i am trying to create a thread in the windows service but i'm getting an error 1607 when trying to execute it. Here's my code

  protected override void OnStart(string[] args)
    {

        string path = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
        using (sw = File.AppendText(path))
        {
            sw.WriteLine("the service has started");

           server = new Server(sw);
           server.start();
           start = true;

            new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = false;
                /* run your code here */
                while (start != true)
                {
                    Thread.Sleep(2000);
                }
                // tcp ip server
                server.started();

            }).Start();

        }
    }

When i'm running it without thread, the OnStart program doesn't end until server ends. Basically when i right click the service and start, the server gets started, and i could connect client as well, but the status doesn't change to "running". It changes to running when the server code stops executing. Here's code for above.

 protected override void OnStart(string[] args)
    {

        string path = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
        using (sw = File.AppendText(path))
        {
            sw.WriteLine("the service has started");

           server = new Server(sw);
           server.start();
           start = true;

           // running without thread
            server.started();
        }
     }

This above code works, but after right clicking and hitting 'Start', status changes to "Running" after server code is done executing (when all clients leave and server shutsdown) not before that. I'm really new to windows service stuff so pardon any stupid mistakes.

Shrey
  • 93
  • 3
  • 5
  • 12
  • The service doesn't change to "Running" in your second example because you're not returning from `OnStart`. The tutorial at https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer clearly says that `OnStart` is supposed to set things up (a thread or a timer, for example) so that the service can monitor asynchronously, and then return. As for error 1607, where are you getting that? On installation? On startup? Have you searched to discover what error 1607 is? – Jim Mischel Dec 23 '17 at 04:53

2 Answers2

2

You have this code in your OnStart method:

    using (sw = File.AppendText(path))
    {
        sw.WriteLine("the service has started");

       server = new Server(sw);
       server.start();
       start = true;

        new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = false;
            /* run your code here */
            while (start != true)
            {
                Thread.Sleep(2000);
            }
            // tcp ip server
            server.started();

        }).Start();
    }

You construct a Server object, passing it a reference to the file you created (i.e. sw). But then you start the thread and exit the using block, which disposes the file.

At some later point, the server object tries to write to the file, but it's been disposed. So the server throws an exception in the thread, which bubbles out to the main program, and craters it.

You'll need to maintain a reference to that file, and close it on OnStop.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Hi Jim, thanks a lot for help. You were right, i was trying to access something that has been disposed already. I removed the using statement and it worked. No more errors :) – Shrey Dec 23 '17 at 07:03
  • @Shey: If this post solved your problem, please click the checkmark to make it the accepted answer. – Jim Mischel Dec 23 '17 at 11:59
0

I think the answer to your question appears here: Windows Service to run constantly

Practically OnStart needs to return fast, and you should start a thread to handle your logic. The status will change to "running" once the OnStart will end, so this is consistent with your observation.

As for why your process ends when running on a different thread - I think that your server shutdown regardless of OnStop - which is the proper way to stop services. From your explanation above I suspect it shutdown (Environment.Exit or similar) once all clients leave.

  • Hey Sasha, thanks for help. The issue is resolved. The 'using' statement is basically used to dispose something off outside the block. I was trying to access something in my thread that had been disposed already. So, all i did to fix the issue is remove 'using' block. And it worked. Really appreciate your help and help of others. Thanks :) – Shrey Dec 23 '17 at 07:06