Program as both service and application?

  • Thread starter Thread starter Rich Bernstein
  • Start date Start date
R

Rich Bernstein

Is it possible to write a .NET service that could also be run as a user
application? This might be something along the lines of defining multiple
entry points, to be used based on how the program is executed.

I would like to do this without creating a DLL for all the program code, and
two executables for the service/application front ends....

Thanks.
 
Rich,
Seeing as the Windows Service defines a Shared Sub Main in your service
class, you should be able to decide to call the either ServiceBase.Run or
Application.Run to either start the service or display a form.

However I would think a common DLL with two EXE front ends would be a
'cleaner' better encapsulated arrangement.

Hope this helps
Jay
 
Doh!
Seeing as this is a C# newsgroup that should be a static void Main.

I was looking at a VB.NET windows service when I wrote it.

Jay

Jay B. Harlow said:
Rich,
Seeing as the Windows Service defines a Shared Sub Main in your service
class, you should be able to decide to call the either ServiceBase.Run or
Application.Run to either start the service or display a form.

However I would think a common DLL with two EXE front ends would be a
'cleaner' better encapsulated arrangement.

Hope this helps
Jay
 
Jay B. Harlow said:
Rich,
Seeing as the Windows Service defines a Shared Sub Main in your service
class, you should be able to decide to call the either ServiceBase.Run or
Application.Run to either start the service or display a form.

However I would think a common DLL with two EXE front ends would be a
'cleaner' better encapsulated arrangement.

Hope this helps
Jay

Yes the hard part is figuring out whether the exe was launched from the
Service Control Manager or by a User. Possible methods include:

- a Command-line switch to run interactivly (easy but inconvienient)
- use win32 API's OpenProcess etc to determine your parent process (hard but
fast)
- use performance counters to determine your parent process (easy but slow)


David
 
Thanks.....

How do I know within the Main function whether or not it was started as a
service, in order to decide which to way to run it? Unfortunately, if I try
ServiceBase.Run, it gives a Windows Service Start Failure and exits (rather
than throwing an exception).

--
-Rich
Jay B. Harlow said:
Rich,
Seeing as the Windows Service defines a Shared Sub Main in your service
class, you should be able to decide to call the either ServiceBase.Run or
Application.Run to either start the service or display a form.

However I would think a common DLL with two EXE front ends would be a
'cleaner' better encapsulated arrangement.

Hope this helps
Jay
 
Rich,
I left that as an exercise for you ;-)

David Browne identified three methods that you can use.

I figured you had an idea of how you knew which way it was started. David's
idea of a command line switch may be the easiest.

Hope this helps
Jay

Rich Bernstein said:
Thanks.....

How do I know within the Main function whether or not it was started as a
service, in order to decide which to way to run it? Unfortunately, if I try
ServiceBase.Run, it gives a Windows Service Start Failure and exits (rather
than throwing an exception).
 
Back
Top