Select Categories Below
Status: Closed Points: 100 Time: 10:20 - Jul 13, 2007
theDude
I need to create a program that can be installed as a windows service, not as a normal desktop program. I'm using .net and C#. How do I do that?
Categories:
Add Categories
When adding more than one category, separate them with commas.
Advertisement
PeterNZ
Date:: Jul 13, 2007
Time:: 17:08
That should be fairly simple! When you create a new project in Visual Studio, you just select Windows Service Project as the project type. This gives you a class whch inherits from System.ServiceProcess.ServiceBase and overrides OnStart(), OnStop(), OnPause(), OnContinue(), and OnShutdown(). The method names should be self explanatory. You add your code into the methods. The on start method is the one where you start your processing. Usually people use a timer there to kick off actions i.e. every 10 minutes or so. The main difference is, you can't just run it i.e. for debugging from inside Visual Studio. You have to add an Installer class. There you can set if the service runs under a user account, starts automatically etc. In order to install the during development, teh easiest method is to use installutil from a Visual Studio Command Prompt. i.e. "installutil /i C:\MyServiceProject\bin\Debug\Servicename.exe" If you need to debug, you have to attach the debugger to the service process. Open the Processes menu from the Debug menu and attach to your service. Let me know if it helped! Cheers Peter
Time:: 17:09
Here is a sample I found in a book: FileMonitor.cs namespace FileMonitor { using System; using System.Collections; using System.Core; using System.ComponentModel; using System.Configuration; using System.Data; using System.Web.Services; using System.Diagnostics; using System.ServiceProcess; using System.WinForms; using System.IO; using Microsoft.Win32.Interop; public class WinService1 : System.ServiceProcess.ServiceBase { private System.ComponentModel.Container components; private System.IO.FileSystemWatcher fileSystemWatcher1; private ObjectList fileWatchers; private System.Diagnostics.PerformanceCounter fDelCtr; private System.Diagnostics.PerformanceCounter fCrCtr; public WinService1() { // This call is required by the WinForms Component Designer. InitializeComponent(); // TODO: Add any initialization after the InitComponent call servicePaused = false; this.CanPauseAndContinue = true; this.CanStop = true; } // The main entry point for the process static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun; // More than one user Service may run within the same process. To add ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WinService1() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container (); this.fileSystemWatcher1 = new System.IO.FileSystemWatcher (); fileSystemWatcher1.BeginInit (); this.fDelCtr = new System.Diagnostics.PerformanceCounter (); this.fCrCtr = new System.Diagnostics.PerformanceCounter (); fileSystemWatcher1.Enabled = true; fDelCtr.CategoryName = "FileMonitorService"; fCrCtr.CategoryName = "FileMonitorService"; fDelCtr.CounterName = "Files Deleted"; fCrCtr.CounterName = "Files Created"; this.ServiceName = "FileMonitor"; fileWatchers = new ObjectList(); fileSystemWatcher1.EndInit (); } /// <summary> /// Set things in motion so your service can do its work. /// </summary> protected override void OnStart(string[] args) { string[] drives = Environment.GetLogicalDrives(); // create filesystem watchers for local fixed drives foreach (string curDrive in drives ) { if ( PlatformInvokeKernel32.GetDriveType(curDrive) == win.DRIVE_FIXED) { int item = fileWatchers.Add( new FileSystemWatcher() ); FileSystemWatcher curWatcher = (FileSystemWatcher) fileWatchers[item]; curWatcher.BeginInit (); curWatcher.Target = System.IO.WatcherTarget.File; curWatcher.IncludeSubdirectories = true; curWatcher.Path = curDrive; curWatcher.Created += new FileSystemEventHandler(OnFileCreated); curWatcher.Deleted += new FileSystemEventHandler(OnFileDeleted); curWatcher.Enabled = true; curWatcher.EndInit(); } } } /// <summary> /// Stop this service. /// </summary> protected override void OnStop() { // reset the counters back to 0 if( fDelCtr.RawValue != 0 ) { fDelCtr.IncrementBy(-fDelCtr.RawValue); } if( fCrCtr.RawValue != 0 ) { fCrCtr.IncrementBy(-fCrCtr.RawValue); } } protected override void OnPause() { servicePaused = true; } protected override void OnContinue() { servicePaused = false; } private void OnFileCreated(Object source, FileSystemEvent
admin
Date:: Aug 22, 2007
Time:: 13:25
theDude, did you get your question solved? If you did then please close this question and distribute the points. If you found the solution on your own, we would be very happy if you could explain it here for the sake other users having the same problem. If you didn't get a solution to your problem, please leave a comment here to let the experts know that you're still looking for an answer. Thanks, The Quomon Admin Team
prashanth.guru
Date:: Oct 18, 2007
Time:: 06:11
using System; using System.Diagnostics; using System.ServiceProcess; namespace WindowsService { class WindowsService : ServiceBase { /// <summary> /// Public Constructor for WindowsService. /// - Put all of your Initialization code here. /// </summary> public WindowsService() { this.ServiceName = "My Windows Service"; this.EventLog.Log = "Application"; // These Flags set whether or not to handle that specific // type of event. Set to true if you need it, false otherwise. this.CanHandlePowerEvent = true; this.CanHandleSessionChangeEvent = true; this.CanPauseAndContinue = true; this.CanShutdown = true; this.CanStop = true; } /// <summary> /// The Main Thread: This is where your Service is Run. /// </summary> static void Main() { ServiceBase.Run(new WindowsService()); } /// <summary> /// Dispose of objects that need it here. /// </summary> /// <param name="disposing">Whether /// or not disposing is going on.</param> protected override void Dispose(bool disposing) { base.Dispose(disposing); } /// <summary> /// OnStart(): Put startup code here /// - Start threads, get inital data, etc. /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { base.OnStart(args); } /// <summary> /// OnStop(): Put your stop code here /// - Stop threads, set final data, etc. /// </summary> protected override void OnStop() { base.OnStop(); } /// <summary> /// OnPause: Put your pause code here /// - Pause working threads, etc. /// </summary> protected override void OnPause() { base.OnPause(); } /// <summary> /// OnContinue(): Put your continue code here /// - Un-pause working threads, etc. /// </summary> protected override void OnContinue() { base.OnContinue(); } /// <summary> /// OnShutdown(): Called when the System is shutting down /// - Put code here when you need special handling /// of code that deals with a system shutdown, such /// as saving special data before shutdown. /// </summary> protected override void OnShutdown() { base.OnShutdown(); } /// <summary> /// OnCustomCommand(): If you need to send a command to your /// service without the need for Remoting or Sockets, use /// this method to do custom methods. /// </summary> /// <param name="command">Arbitrary Integer between 128 & 256</param> protected override void OnCustomCommand(int command) { // A custom command can be sent to a service by using this method: //# int command = 128; //Some Arbitrary number between 128 & 256 //# ServiceController sc = new ServiceController("NameOfService"); //# sc.ExecuteCommand(command); base.OnCustomCommand(command); } /// <summary> /// OnPowerEvent(): Useful for detecting power status changes, /// such as going into Suspend mode or Low Battery for laptops. /// </summary> /// <param name="powerStatus">The Power Broadcast Status /// (BatteryLow, Suspend, etc.)</param> protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) { return base.OnPowerEvent(powerStatus); }
greeshma593
Date:: Apr 24, 2008
Time:: 03:58
sir,i am a new person to this field.i want to create a small program like popupmesage getting displayed at few seconds.which i can keep as a window services .it must be in Csharp. hope u will help me
Question Answered
This question has been closed, and points have been rewarded to the following experts:
You're welcome however to comment or give additional information or if you wish, you have the ability to write an Answer Summary for the Summary Area.
Answer this Question
New User
Email:
Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address
Registered User
Username:
Password:
Forgot Your Password?
Enter your email address below and we will resend your login information to you.
Login Information Sent
Questions
Please Help! My windows service has permissions p...
From a windows .net application is it possible to ...
about socket communication in Windows service
how can I cause my application to wait and then re...
You have 100 characters to use
Rank
Expert
Points
1.
942
2.
410
3.
388
4.
125
5.
99
6.
50
7.
8.
45
9.
38
10.
37
Register today to share your knowledge with the community and be recognized and rewarded for your contributions.
Register Here
"Psst, Quomon is a great site. Pass it on." Tell a Friend | Link To Us | Save to Delicious | Digg it
Language Options
English:
Español:
Sponsors
Questions and Answers Software Real Estate Postcards Marketing Fulfillment