I am beginner to .NET.
I have a question regarding windows service application running multi-thread. My question is when I tried to register my application into windows service, I see my service status in “starting” in the service windows. I have included few lines code to show what I am trying to do.
protected override void OnStart(string [] args) {
timer = Timer(5000);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Start();
// when I commented out Application.Run() it runs perfect.
Application.Run(); // run until all the threads finished working
//todo
}
private void OnElapsedTime(object s, ElapsedEventArgs e) {
SmartThreadPool smartThreadPool = new SmartThreadPool();
while( i < numOfRecords){
smartThreadPool.QueueWorkItem(DoWork);
//.....
}
}
If you need further information please let me know.
Application.Run()in the context you’ve used it just tells the service to run itself again in the same application context. As a part of your Windows Service, an application context already exists within the context of yourServiceBase. Since it’s a service, it won’t stop until it’s given an instruction to stop through either a method that requires it, an unhandled exception or an external command.If you’re concerned about preventing the stop from occurring while threads are in the midst of executing, you’ll need a global lock of some sort indicating processes are working. It might be as simple as elevating the scope of your
SmartThreadPool: