views:

37

answers:

2

Hi, My Windows service is able to launch threads (suing the ThreadStart delegate) in Win XP, but in Win 2003 Server it cant, it is not throwing an exception too ... the thread is simply not starting.

I made a testing Windows Service which have the same code in the (OnStart) event handler and it worked both on Win XP and Win 2003 Server, that is driving me crazy, I dont know what is wrong with my original service, why it cant start the thread.

here is the code in both my Win Service with the problem and in the testing Win Service which worked just fine:

    private Thread trd;
    StreamWriter sw;
    int i = 0;


    protected override void OnStart(string[] args)
    {
        // TODO: Add code here to start your service.
        sw = new StreamWriter("c:\\TestingService.txt", true);

        trd = new Thread(new ThreadStart(this.LoopingThread));
        trd.IsBackground = false;
        trd.Priority = ThreadPriority.Highest;
        trd.Start();
    }

    protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down necessary to stop your service.
    }

    private void LoopingThread()
    {
        while (i < 100)
        {
            lock (sw)
            {
                sw.WriteLine("hello from thread i="+i.ToString());
                sw.Flush();
            }
            i++;
            Thread.Sleep(1000);
        }
    }

this code is "exactly" identical on both Win Services. my Original Service (which have the problem) got many references to other DLLs, and its "Using" list is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Xml;
using System.Security.Principal;
using System.Reflection;
using System.Threading;
using System.Management;

and other using statements that is related to some confidential DLLs (3rd parties) but I am not actually creating any object ... the effective code is just what I posted up.

I cant figure out why my Win Service cant launch Threads on Win 2003 Server

+1  A: 

Put a call to System.Diagnostics.Debugger.Break() at the beginning of your OnStart() method and compile in debug. When you start the service, you will be prompted to start a debugging session. Once in the debugger, open the Exceptions dialog from the Debug menu and check the Thrown column for the Common Language Runtime Exceptions. Your service will halt if an exception is thrown.

If I had to guess, I'd say that the reason your thread is not starting is because it doesn't make it that far. Based on the code your provided, I'd say the creation of the StreamWriter is failing for some reason. For example, you may not have write permissions to the C drive on the Win 2003 Server machine.

Matt Davis
A: 

This was solved in a very silly way !! I just created another class in my Windows Service, copied all the code to it, then made the code in program.cs create in instance of that class instead of the old service class.

everything worked fine after that, I don't know what happened !!

Thanks for all those who tried to help

TB