Link to home
Start Free TrialLog in
Avatar of RodionP
RodionP

asked on

Windows service won't run multiple threads

Hi,

I have a little problem:

I am trying to run deploy a windows service (I am using .msi installer to take care of widnows service deployment) and my service is supposed to start many threads
(number of threads depends on the configuration). The problem is when I try to start my service it won't start, and threads won't start. When I try to run this app as windows app, threads start without a problem.

Thank you
Avatar of RodionP
RodionP

ASKER

Here is the sample code that I'm running:


public class EngineServer:ServiceBase
{
      private IInterfaceLog log;

      #region Constructor / Destructor
      public EngineServer():base()
      {
            InitializeComponent();
            this.log = new LogProvider();
      }

      private void InitializeComponent()
      {
            this.ServiceName = "RbInterface_engine_service";
      }

      protected override void Dispose( bool disposing )
      {
            if( disposing )
            {
                  
            }
            base.Dispose( disposing );
      }
      #endregion

      static void Main(string[] args)
      {
            ServiceBase.Run(new EngineServer());
      }

      protected override void OnStart(string[] args)
      {
            Engine.Instance.EngineMessagePosted += new ThreadPostHandler(OnEngineMessage);
            Engine.Instance.Start();
      }
 
      protected override void OnStop()
      {
            Engine.Instance.Stop();
      }

      [MethodImpl(MethodImplOptions.Synchronized)]
      private void OnEngineMessage(string message, string threadId, int cycleCount, string instanceType, string assembly, string function)
      {
            //Do some logging
      }
}


From the above code, Engine.Instance.Start() function has the following code, the funny part if I comment out:
            //Engine.Instance.EngineMessagePosted += new ThreadPostHandler(OnEngineMessage);
            //Engine.Instance.Start();
it works without a problem, it doesn't start any threads, but service starts.

[MethodImpl(MethodImplOptions.Synchronized)]
public void Start()
{
      this.ReloadConfig();
      lock(syncObject)
            this.runningThreads.Clear();
      foreach(string threadId in this.threadsToExecute.Keys)
      {
            Thread newThread = new Thread(new ThreadStart(this.RunThread));
            newThread.Priority = ThreadPriority.Lowest;
            newThread.Name = threadId;
            lock(syncObject)
                  this.runningThreads[threadId] = newThread;
      }
      lock(syncObject)
            this.isContinue = true;
      this.PostMessage("Starting Engine");
      lock(syncObject)
      foreach(Thread thread in this.runningThreads.Values)
            thread.Start();
}


Any help is highly appreciated
ASKER CERTIFIED SOLUTION
Avatar of honzaf
honzaf

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial