Pages

Running Window Application using Window Service

How to run window apllication in specfic interval from window service 1)Create window service application(Assume Creation of window service known) 2)Use Namespace system .Timer add timer.start in start method of windowservice 3)Add Time elapsed event handler mechanism in start method of window service 4)In Timeelapsed event add following line code Process proc; proc = new Process(); proc.StartInfo.FileName = @"C:\windowapplicationName.exe"; proc.StartInfo.Arguments = "jhgj";[optional] proc.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; proc.Start(); 5)After install service in service controler right click on service name then click properties under login tab enable Allow service to interact with desktop (For Automatic enable review code given below add name space Microsoft.win32 in code) 6)Start service
Declartion in Class Member Section /////////// Process p = new Process(); // int ncount = 0; int iProcessID = 0; System.Timers.Timer objtimer = new Timer(1000); ////////// Code for start Method ////////// this.objtimer.Enabled = true; this.objtimer.Start(); this.objtimer.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); //Automatatically enable window service for inter active desktop // Here is where we set the bit on the value in the registry. //Reference from CodeProject.com // Grab the subkey to our service RegistryKey ckey = Registry.LocalMachine.OpenSubKey( @"SYSTEM\CurrentControlSet\Services\Sample", true); // Good to always do error checking! if (ckey != null) { // Ok now lets make sure the "Type" value is there, //and then do our bitwise operation on it. if (ckey.GetValue("Type") != null) { ckey.SetValue("Type", ((int)ckey.GetValue("Type") | 256)); } } ///////// Code for Timer Elapsed event ///////// ///here if condtion are used to check not to run same process again if process is already running try { if (iProcessID != 0) { Process[] objProcesses = System.Diagnostics.Process.GetProcesses(); bool IsExists = false; foreach (Process prc in objProcesses) { if (prc.Id == iProcessID) { IsExists = true; } } if (IsExists == false) iProcessID = 0; } if (iProcessID == 0) { Process proc; proc = new Process(); proc.StartInfo.FileName = @"C:\TrayDemo.exe"; proc.StartInfo.Arguments = "jhgj"; proc.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; if (proc.Start()) { iProcessID = proc.Id; } } } catch (Exception ex) { //MessageBox.Show(ex.ToString()); } /////////////