-1
protected override void OnStart(string[] args)
{
 while (true)
 {
 // do some work

  // idle
  Thread.Sleep(0, interval, 0)
 }
}

I am unable to use timer/thread for my service, because my third party dll is not working, so I have tried the while condition in OnStart it throws error. How will i achieve the endless loop without timer/thread.

Jagath Karra
  • 87
  • 1
  • 2
  • 8
  • 1
    OnStart is for fast operations only. Windows requires to get back the handle of the process and that's done after OnStart finishes, so if it determines you're taking too long, it will kill your service. You *have to* use a Timer, Thread, Task, BackgroundWorker or whatever method suits you best to continue doing operations – Camilo Terevinto Feb 20 '19 at 12:04
  • @CamiloTerevinto I have used the other class method in the onstart, in that method having endless loop. I cant use the thread /timer kind that third party dll is not working. – Jagath Karra Feb 20 '19 at 12:39
  • That's exactly the same, don't you think? – Camilo Terevinto Feb 20 '19 at 12:40
  • @CamiloTerevinto I know, I have created [STAThread] but it throws error, I have asked third-party (Act6530xcom.dll) they said they haven't created for windows service support. – Jagath Karra Feb 22 '19 at 08:11

1 Answers1

0

You will have to do the work on another thread, or the windows service manager will assume your service hasn't started because the OnStart() never returns.

Your third-party library shouldn't work any differently, however the thread may require a different apartment model. Try creating an STA thread (see Starting an STAThread in C#) and seeing if that works, otherwise talk to the creator of the library.

Ananke
  • 1,250
  • 9
  • 11
  • I have created [STAThread] but it throws error, I have asked third-party (Act6530xcom.dll) they said they haven't created for windows service support. – Jagath Karra Feb 22 '19 at 08:08