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
//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
Jim JonesPosted Mar 15, 2010, 12:24 PM
What finally worked was to move the main out of Program.cs into the partial class with the default constructor and OnStop and OnStart. Then I dump the Program.cs from the Project.
Then I moved the OnStop and OnStart methods into the designer partial class. Now the only constructor is the one I wrote in the designer partial class which takes no parameters. Also, I now call InitializeComponent() from that constructor (at the end).
All the other methods I have work fine, the number of processor threads is configurable from the app.config file.
Hope that helps someone.
Jim