CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    341

    Windows services with C#

    I found some articles that describe how to create windows services with VB.Net. Is there any that shows how to do it with C#??

    Thank you

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Windows services with C#

    You should be able to translate the VB.NET code into C# easily enough.

    If not, here's a little snippet I wrote ages back to test services :

    Code:
    using System;
    using System.Collections;
    using System.Configuration.Install;
    using System.ServiceProcess;
    using System.ComponentModel;
    
    class UserService : System.ServiceProcess.ServiceBase
    {
    	public UserService()
    	{
    		this.AutoLog = true;
    		this.CanStop = true;
    		this.CanPauseAndContinue = false;
    		this.ServiceName = "TestService";
    	}
    
    	protected override void OnStart(string [] args)
    	{
    	}
    
    	protected override void OnStop()
    	{
    	}
    }
    
    class App
    {
    	[STAThread]
    	static public void Main()
    	{
    		System.ServiceProcess.ServiceBase.Run(new UserService());
    	}
    }
    
    [RunInstallerAttribute(true)]
    public class UserServiceInstaller : Installer
    {
    	private ServiceInstaller serviceInstaller;
    	private ServiceProcessInstaller processInstaller;
    
    	public UserServiceInstaller()
    	{
    		serviceInstaller = new ServiceInstaller();
    		processInstaller = new ServiceProcessInstaller();
    
    		processInstaller.Account = ServiceAccount.LocalSystem;
    
    		serviceInstaller.StartType = ServiceStartMode.Manual;
    		serviceInstaller.ServiceName = "TestService";
    
    		Installers.Add(serviceInstaller);
    		Installers.Add(processInstaller);
    	}
    }
    Compile this up and run

    Code:
    InstallUtil <executable with '.exe'>
    to install it into the services list, and

    Code:
    InstallUtil /u <executable with '.exe'>
    to uninstall.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    341

    Re: Windows services with C#

    Thanks again

    You are very helpfull!

    Wish you the best


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured