473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C# run Windows Service project as regular Windows application

In C++ you can allow your Windows program to act as a Windows Service by
implementing some functions required by the Service Control Manager. This
same application could be written in such a way that the application could
run as a regular Windows Application or as a Windows Service. How can I do
this in C#? I am using Visual Studio 2003.
Jul 5 '06 #1
3 3149
Trevor,

The models for an application and a service are different from each
other. For example, an app usually requires some sort of user interaction,
whereas services are interacted with in a different manner. Also,
applications usually have UI, whereas a service will not.

What you should do is separate your business logic (not involving UI)
into another assembly, then have the Windows Application and the Service
project reference it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Trevor" <tr****@spam.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
In C++ you can allow your Windows program to act as a Windows Service by
implementing some functions required by the Service Control Manager. This
same application could be written in such a way that the application could
run as a regular Windows Application or as a Windows Service. How can I
do this in C#? I am using Visual Studio 2003.

Jul 5 '06 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2***************@TK2MSFTNGP03.phx.gbl...
Trevor,

The models for an application and a service are different from each
other. For example, an app usually requires some sort of user
interaction, whereas services are interacted with in a different manner.
Also, applications usually have UI, whereas a service will not.

What you should do is separate your business logic (not involving UI)
into another assembly, then have the Windows Application and the Service
project reference it.
I was afraid it would come down to this. Thanks for taking the time to
respond.
Jul 5 '06 #3
Hi Trevor,
While I agree with Nicholas Paldino's comments, I have done what you
are asking for testing purposes. If the project is based on the .NET
Service template, you can just call the service's OnStart() method from
Main(), something like this:

static void Main()
{
bool runInteractive = true;
if (runInteractive)
{
MyServiceClass svc = new MyServiceClass();
svc.StartInteractive();
return;
}
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//

ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
MyServiceClass () };

System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
}

and in MyServiceClass, just add this function:

public void StartInteractive()
{
OnStart(null);
}

Hope this helps,
John

Jul 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jan Eliasen | last post by:
Hi I am writing a Windows Service in VB.NET I read a walkthrough on the net about how to do it, and it states that I should use the "installutil" that comes with the .NET framework to get the...
5
by: clsmith66 | last post by:
I've been asked to find out if a project is possible, but I'm not having much luck finding the information I need, I hope some one can help. I need to see if I can build a windows service on the...
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
8
by: vtxr1300 | last post by:
I've followed 2 very similar tutorials on creating a windows service and an installer. But, after successfully compiling it, I try to do the InstallUtil command, but I get a...
5
by: Bill Nguyen | last post by:
I have a small VB app that I would like to install as a service in 1 of our Win 03 servers. Any suggestion is geatly appreciated. Bill
4
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to...
3
by: illegal.prime | last post by:
Hi all, I have a service that needs to start a regular windows application. I'm running the service as ServiceAccount.LocalSystem. But, when it starts the process (using Process.Start) the GUI...
0
by: =?Utf-8?B?U2ltb25EZXY=?= | last post by:
Hi All I would like to install the same Windows Service project on the same server under different names, one for each customer. I have been able to do it but I would like an expert opinion as...
5
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.