Jim Jones

Jim Jones

  • NA
  • 10
  • 0

Service startup problem

Mar 9 2010 1:31 PM

Have written several Windows services in C# and they all run fine. Decided to make them multithreaded. Am havin problems with getting it to start as a service, keep getting a TypeInitializationException, which I'm sure comes from the service specific startup. Am using the Environment.UserInteractive property to either start as a service or as a console app.  It runs fine as a console app.
    First, a word on the architecture. The app has a main class which loads work from a database table, spans worker threads, and reloads them. Once the worker threads are cycling through the process, reload, process cycle the main class is idle, it's loadProcessor method is the callback for the threadpoolexecutor when a processor as a thread ends. the loadProcessor method checks the internal work queue each time a processor comes through for reload. If the queue is empty it starts a count. When the count equals the number of worker threads it calls a method that encapsulates the main class in a thread and starts it. The main class run method tries to load the internal queue and if successful loads the processors and goes dormant. If not it sleeps and tries again.
My main method looks like this:
[code]
static void Main()
{
    if(! Environment.UserInteractive)
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]{new ThreadedUtility()};
        ServiceBase.RunIServicesToRun);
    }
    else
    {
        ThreadedUtility tu = new ThreadedUtility();
        tu.createUtility();
        tu.startUtility();
    }
}
[/code]
Note that in the service section I do not have a reference to the main class so I can't call the other two method there. Unless it's possible to get one somehow?
These methods are as follows:
[code]
public void createUtility()
{
    //Create a locking object to synchronize the loadProcessor method
    pLock = new Object();
    //Initialize the internal work queue
    this.queue = new Stack<MonitoredFile>();
    //Initialize the static data access layer
    DatabaseImple.setConnectionString();
    //Initialize the static logger
    Logger.setLogs();
    // Create the threadpool executor
    this.executor = new ThreadPoolExecutor(this);
    createProcessors();
    this.count = 0;
}
[/code]
These are all instance variables of the ThreadedUtility class. A reference to the processors is held in an array also an instance property. Each processor has a unique index.
The startUtil() method is:
[code]
public void startUtil()
{
    // utility is an instance variable of type Thread
    utility = new Thread(new ThreadStart(processFiles));
    utility.Start();
}
[/code]
So my question boils down to where is a good place to put the calls to these methods in the start up sequence.
Things I've tried:
Putting them in the constructor with InitializeComponent(), no luck error as above.
Putting them in the OnStart(string[] args) same result same error.
Putting them in the InitializeComponent() method. Did this before and it worked, know that it screws up the Design view, however didn't work this time, same error.
Any ideas?
Jim

Answers (1)