Window Service not executing the console application exe

May 20 2014 9:12 AM
I have created a console application and call that console application in the window service but the console application exe is not executing by the window service. This conlole application should be executed when timer react on its time i.e. every 1 minute.

Please help me.

The code is as follows.

Window service code:
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.Configuration;
using System.IO;
using System.Timers;

namespace FileWatcherWindowsService
{
    public partial class FileWatcherWinService : ServiceBase
    {
        Timer objTimer = null;

        public FileWatcherWinService()
        {
            InitializeComponent();
            objTimer = new Timer();
        }

        private void TraceService(string content)
        {
            FileStream fs = null;
            StreamWriter sw = null;
            try
            {
                //set up a filestream
                fs = new FileStream(ConfigurationManager.AppSettings["LogFilePath"] + "FileWatcherServiceLog_" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", FileMode.OpenOrCreate, FileAccess.Write);

                //set up a streamwriter for adding text
                sw = new StreamWriter(fs);

                //find the end of the underlying filestream
                sw.BaseStream.Seek(0, SeekOrigin.End);

                //add the text and add the text to the underlying filestream
                sw.WriteLine(content);

                // flush the streamwriter after adding text
                sw.Flush();

                //close the writer
                sw.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            try
            {
                System.Diagnostics.Debugger.Launch();
                TraceService("File Watcher Console Application Started At " + DateTime.Now);
                //System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + "FileWatcherConsoleApplication.exe");
                System.Diagnostics.Process.Start(new ProcessStartInfo(System.AppDomain.CurrentDomain.BaseDirectory + "FileWatcherConsoleApplication.exe"));
                TraceService("-------------------------");
            }
            catch (Exception ex)
            {
                TraceService("Exception Occured:");
                TraceService("-------------------------");
                TraceService(ex.Message);
                throw ex;
            }
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                //add this line to text file during start of service
                TraceService("File Watcher Window Service Started First Time At " + DateTime.Now.ToString());
                TraceService("-------------------------");
                //handle Elapsed event
                objTimer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

                //This statement is used to set interval to 1 minute (= 60,000 milliseconds)
                objTimer.Interval = 60000;

                //enabling the timer
                objTimer.Enabled = true;
            }
            catch (Exception ex)
            {
                TraceService("Exception Occured:");
                TraceService("-------------------------");
                TraceService(ex.Message);
                throw ex;
            }
        }

        protected override void OnStop()
        {
            try
            {
                objTimer.Enabled = false;
                TraceService("stopping service");
            }
            catch (Exception ex)
            {
                TraceService("Exception Occured:");
                TraceService("-------------------------");
                TraceService(ex.Message);
                throw ex;
            }
        }

        private void fswFileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            try
            {
                System.Diagnostics.Debugger.Launch();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void fswFileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            try
            {
                System.Diagnostics.Debugger.Launch();
                System.IO.File.Copy(e.FullPath, "E:\\Archive_Directory\\" + e.Name);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void fswFileWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            try
            {
                System.Diagnostics.Debugger.Launch();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void fswFileWatcher_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            try
            {
                System.Diagnostics.Debugger.Launch();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}


Answers (2)