0

I have a program that is written to run in the foreground. But it backgrounds itself on some machines.

Here is the context.

I have a class derived from ServiceBase,

public class MyService: ServiceBase

For debug purposes, I do not start the service as follows:

ServiceBase.Run(myService);

Instead, I invoke the OnStart directly as follows, and sleep forever:

myService.OnStart(null);
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

For some reason the process is backgrounding itself on the production machine. Ideas?

Ziffusion
  • 8,779
  • 4
  • 29
  • 57

2 Answers2

1

Turns out this was because VS sets OutputType of a service project to WinExe (as opposed to Exe for a console application). WinExe executables background themselves. And the behavior seemed to be different on two machines, because on one I was in the cygwin shell, where, for some reason, it does not background itself.

Ziffusion
  • 8,779
  • 4
  • 29
  • 57
  • 1
    You might be interested in [my question here](http://stackoverflow.com/questions/4028353/where-do-writes-to-stdout-go-when-launched-from-a-cygwin-shell-no-redirection) which explores detection of being launched from a console (win32 cmd.exe vs cygwin vs other applications). – Ben Voigt Jan 14 '11 at 19:43
0

I believe that Windows actually does or can pay-attention to the status of the 'main' thread running your application. If it sees that the 'main' thread is sleeping, then IIRC it can background the whole process. The choice of when or what to background often is related to energy-saving features, such as on a laptop.

Brent Arias
  • 29,277
  • 40
  • 133
  • 234