Best Timer for using in a Windows service

Both System.Timers.Timer and System.Threading.Timer will work for services. The timers you want to avoid are System.Web.UI.Timer and System.Windows.Forms.Timer, which are respectively for ASP applications and WinForms. Using those will cause the service to load an additional assembly which is not really needed for the type of application you are building. Use System.Timers.Timer like the following … Read more

Inno Setup for Windows service?

You don’t need installutil.exe and probably you don’t even have rights to redistribute it. Here is the way I’m doing it in my application: using System; using System.Collections.Generic; using System.Configuration.Install; using System.IO; using System.Linq; using System.Reflection; using System.ServiceProcess; using System.Text; static void Main(string[] args) { if (System.Environment.UserInteractive) { string parameter = string.Concat(args); switch (parameter) { … Read more

How to check if a service is running via batch file and start it, if it is not running?

To check a service’s state, use sc query <SERVICE_NAME>. For if blocks in batch files, check the documentation. The following code will check the status of the service MyServiceName and start it if it is not running (the if block will be executed if the service is not running): for /F “tokens=3 delims=: ” %%H … Read more

System.BadImageFormatException: Could not load file or assembly (from installutil.exe)

Some more detail for completeness in case it helps someone… Note that the most common reason for this exception these days is attempting to load a 32 bit-specific (/platform:x86) DLL into a process that is 64 bit or vice versa (viz. load a 64 bit-specific (/platform:x64) DLL into a process that is 32 bit). If … Read more

Automatically start a Windows Service on install

In your Installer class, add a handler for the AfterInstall event. You can then call the ServiceController in the event handler to start the service. using System.ServiceProcess; public ServiceInstaller() { //… Installer code here this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall); } void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e) { ServiceInstaller serviceInstaller = (ServiceInstaller)sender; using (ServiceController sc = new ServiceController(serviceInstaller.ServiceName)) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)