none
Windows service calling javaw.exe at start of computer, further it is checking that task exists in task manager or not, this code have issues RRS feed

  • General discussion

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Timers;
    using System.IO;
    using System.Threading.Tasks;
    namespace javawExeStart
    {
    public partial class Scheduler : ServiceBase
    {
    private Timer timer1;
    //private ProcessStartInfo ProcessInfo;
    public Scheduler()
    {
    InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
    var javaws = File.Exists(@"C:\Program Files\Java\jre6\bin\javaws.exe") ? @"C:\Program Files\Java\jre6\bin\javaws.exe" : @"C:\Program Files (x86)\Java\jre6\bin\javaws.exe";
    var psi = new ProcessStartInfo(javaws);
    foreach(Process p in Process.GetProcessesByName("javaw"))
    {
    p.WaitForExit();
    if (p.ProcessName == "javaw.exe")
    {
    //make sure child process is already started
    Process.Start(psi).WaitForExit();
    }
    else
    {
    try
    {
    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"C:\himanshu\scripts\jar_start.bat");
    psi.RedirectStandardOutput = true;
    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    psi.UseShellExecute = false;
    System.Diagnostics.Process listFiles;
    listFiles = System.Diagnostics.Process.Start(psi);
    System.IO.StreamReader myOutput = listFiles.StandardOutput;
    listFiles.WaitForExit(2000);
    if (listFiles.HasExited)
    {
    string output = myOutput.ReadToEnd();
    Library.WriteErrorLog(output.ToString());
    }
    }
    catch (NotImplementedException ex)
    {
    Library.WriteErrorLog(ex.ToString());
    }
    }
    }
     
    //start javaw process on windows service start
    timer1 = new Timer();
    this.timer1.Interval = 30000; //30 sec
    this.timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
    timer1.Enabled = true;
    Library.WriteErrorLog("The windows service is started successfully");
    }
    //"C:\Program Files\Java\jre6\bin\javaw.exe" -jar "C:\HP\AHCMail\AHC-2.9\AHCheckr.jar"
    void timer1_Elapsed(object sender, ElapsedEventArgs e)
    {
    //string command = @"C:\himanshu\scripts\jar_start.bat";
    //ProcessInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
    Library.WriteErrorLog("The windows service is Ticked successfully");
    }
    protected override void OnStop()
    {
    timer1.Enabled = false;
    Library.WriteErrorLog("The windows service is stopped succssfully");
    }
    }
    }
     
    

    I have created one windows service and onstart calling javaw.exe file from one batch script and I need to check this task is there in task manger or not every 30 seconds

    there code have some issues regarding getting current process exists check and not starting batch script at start

    Tuesday, December 23, 2014 7:00 PM

All replies