Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailemVyřešeno .NET service (Dotaz na Wikana)

Ahoj,

mám službu (service) a potřebuji, aby se periodicky spouštěl proces.
Protože je nutné zabránit souběhu, je služba využita s proc.WaitForExit();

Bohužel proces sice funguje, ale nedá se řídit (zastavit/spustit) regulérně.
Nelekej se délky kódu, pes je zakopaný jen v metodě OnStart
Konkrétně mám:

  public class ServiceImplementation : IWindowsService
    {

        bool sluzbaJede;
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            sluzbaJede = false;
        }

        /// <summary>
        /// This method is called when the service gets a request to start.
        /// </summary>
        /// <param name="args">Any command line arguments</param>
        public void OnStart(string[] args)
        {
            Console.WriteLine("Prohledavam databazi...");

            sluzbaJede = true;
            ProcessStartInfo start = new ProcessStartInfo();
            // Enter in the command line arguments, everything you would enter after the executable name itself
            start.Arguments = "-f d:\\Programy\\EasyPHP-Devserver-17\\eds-www\\telnet\skript.php";
            // Enter the executable to run, including the complete path
            start.FileName = "d:\\Programy\\EasyPHP-Devserver-17\\eds-binaries\\php\\php5630vc11x86x190305170114\\php.exe";
            // Do you want to show a console window?
            start.WindowStyle = ProcessWindowStyle.Hidden;
            start.CreateNoWindow = true;
            int exitCode;


            // Run the external process & wait for it to finish
            using (Process proc = Process.Start(start))
            {
                proc.WaitForExit();
                // Retrieve the app's exit code
                exitCode = proc.ExitCode;
            }

            Console.WriteLine("Prohledavani dokonceno.");

            if (sluzbaJede == true)
            {
                this.OnStart(null);
            }

        }

        /// <summary>
        /// This method is called when the service gets a request to stop.
        /// </summary>
        public void OnStop()
        {
            sluzbaJede = false;
        }

        /// <summary>
        /// This method is called when a service gets a request to pause,
        /// but not stop completely.
        /// </summary>
        public void OnPause()
        {
            sluzbaJede = false;
        }

        /// <summary>
        /// This method is called when a service gets a request to resume 
        /// after a pause is issued.
        /// </summary>
        public void OnContinue()
        {
            sluzbaJede = true;
        }

        /// <summary>
        /// This method is called when the machine the service is running on
        /// is being shutdown.
        /// </summary>
        public void OnShutdown()
        {
            sluzbaJede = false;
        }

        /// <summary>
        /// This method is called when a custom command is issued to the service.
        /// </summary>
        /// <param name="command">The command identifier to execute.</param >
        public void OnCustomCommand(int command)
        {
        }
    }
}

Tuším, kde je potíž. Vadí mu nějakým způsobem to, kdy se proces OnStart neukončí. (Ano, tak je to napsané).

Lze z toho nějak jednoduše ven, aniž bych se musel prokousávat tunou materiálů o službách? :-)

Děkuji ! Děkuji za spolupráci, přátele,
Nakonec jsem to doňuchal nějak takhle:

 public class ServiceImplementation : IWindowsService
    {
        private static Timer Timer;
        bool sluzbaJede;
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            sluzbaJede = false;
            Timer.Stop();
        }

        /// <summary>
        /// This method is called when the service gets a request to start.
        /// </summary>
        /// <param name="args">Any command line arguments</param>
        public void OnStart(string[] args)
        {
            Timer = new Timer(2000); // every 40 seconds
            Timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            Timer.Start(); // <- important

            

        }

        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Timer.Stop();
            Console.WriteLine("Prohledavam databazi...");

            sluzbaJede = true;
            ProcessStartInfo start = new ProcessStartInfo();
            // Enter in the command line arguments, everything you would enter after the executable name itself
            start.Arguments = "-f d:\\Programy\\EasyPHP-Devserver-17\\eds-www\\telnet\\skript.php";
            // Enter the executable to run, including the complete path
            start.FileName = "d:\\Programy\\EasyPHP-Devserver-17\\eds-binaries\\php\\php5630vc11x86x190305170114\\php.exe";
            // Do you want to show a console window?
            start.WindowStyle = ProcessWindowStyle.Hidden;
            start.CreateNoWindow = true;
            int exitCode;


            // Run the external process & wait for it to finish
            using (Process proc = Process.Start(start))
            {
                proc.WaitForExit();
                // Retrieve the app's exit code
                exitCode = proc.ExitCode;
            }

            Console.WriteLine("Prohledavani dokonceno.");

            Timer.Start();
        }

        /// <summary>
        /// This method is called when the service gets a request to stop.
        /// </summary>
        public void OnStop()
        {
            sluzbaJede = false;
            Timer.Stop();


...... A tak dále..........
        }

Odpověď na otázku

1 Zadajte svou přezdívku:
2 Napište svou odpověď:
3 Pokud chcete dostat ban, zadejte libovolný text:

Zpět do poradny