473,837 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3166
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.co m

"Trevor" <tr****@spam.co mwrote in message
news:%2******** ********@TK2MSF TNGP04.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.c omwrote in
message news:%2******** *******@TK2MSFT NGP03.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.StartIntera ctive();
return;
}
System.ServiceP rocess.ServiceB ase[] 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.ServiceP rocess.ServiceB ase[] {new
Service1(), new MySecondUserSer vice()};
//

ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new
MyServiceClass () };

System.ServiceP rocess.ServiceB ase.Run(Service sToRun);
}

and in MyServiceClass, just add this function:

public void StartInteractiv e()
{
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
1587
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 Windows Service running as a service. What if I'd like to install the Windows Service on a machine that does not have .NEt framework installed? Does anyone have an idea?
5
3413
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 fly customized to a specific client. Is it possible to run the VS command line tool through another application (VB.NET)? I'm thinking I can run the tools as a process and pass the necessary parameters to the process, but I'm not sure how to go...
2
6904
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 (which is the UI used to adjust the actions taken by, and the schedule of the service), then a privileged user thread should be used in the UI - no service required. But... "A windows service enables the creation of long-running executable
8
7221
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 badimageformatexception. I tried to do some searching on that exception, but all I could find was a small blurb on MSs site about C++ which I'm not using. Can anyone see a problem in this code? Thanks. using System; using System.Collections.Generic; using...
5
3012
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
4191
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 check to see if the status is in stopped or running mode. But that doesn't tell me if it is actually running. I need to know this so that if it happens I can programmatically start the same service on another machine.
3
6145
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 for the application doesn't appear - however, I can see the process in the TaskManager. Now, feel free to mention that starting a regular windows application
0
2701
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 to whether my solution is robust or whether there is a better way to do it. What I've been trying to do is to create a core project with different extension projects, one for each customer. I wanted to create a different Setup project for each...
5
3315
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? Why do I need to set a property within my code to the service name? Are all these required or am I just doing this for consistency purposes?
0
9843
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10877
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10570
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10626
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10273
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9402
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7001
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5670
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.