
 Služba v C# - NET otázka ohledně stability služby
				Služba v C# - NET otázka ohledně stability služby			
							
					Dobrý den,
potřebuji nutně konzultovat službu.
Služba se periodicky dotazuje databáze a je nezbytné, aby fungovala jednou a korektně.
Je napsaná v .NET C#
Účelem je spustit PHP skript, který spustí následně program B, který trvá různou dobu.
Jakmile se spuštěný program B dokončí, je žádoucí, aby služba zafungovala znovu a program B opět spustila.
A tento program B fungoval opět nějakou dobu.
Funguje to ovšem pouze nějakou dobu skvěle, ale pak program B přestane korektně pracovat.
Žádoucí je, aby nikdy nedošlo ke dvojitému spuštění programu B.
Bohužel se stává něco nežádoucího, kdy spuštěný program B kolabuje.
Poměrně jednoduchá služba:
 
public class ServiceImplementation : IWindowsService
 {
  private static Timer Timer;
        
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            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 2 seconds
            Timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            Timer.Start(); // <- important
        }
        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (Timer.Enabled == true)
                Timer.Stop();
           
            
            ProcessStartInfo start = new ProcessStartInfo();
            // Enter in the command line arguments, everything you would enter after the executable name itself
            start.Arguments = "-f c:\\Programy\\EasyPHP-Devserver-17\\eds-www\\program.php";
            // Enter the executable to run, including the complete path
            start.FileName = @"C:\Programy\EasyPHP-Devserver-17\eds-binaries\php\php5630vc11x86x200506203219\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;
            }
           
            if( Timer.Enabled == false)
            Timer.Start();
        }
        /// <summary>
        /// This method is called when the service gets a request to stop.
        /// </summary>
        public void OnStop()
        {
            
            Timer.Stop();
        }
        /// <summary>
        /// This method is called when a service gets a request to pause,
        /// but not stop completely.
        /// </summary>
        public void OnPause()
        {
            
            Timer.Stop();
        }
        /// <summary>
        /// This method is called when a service gets a request to resume 
        /// after a pause is issued.
        /// </summary>
        public void OnContinue()
        {
            
            Timer.Start();
        }
        /// <summary>
        /// This method is called when the machine the service is running on
        /// is being shutdown.
        /// </summary>
        public void OnShutdown()
        {
            Timer.Stop();
        }Vidí tady někdo nějaký problém, který zapřičiní, že se program B (ve skriptu program.php) může spustit vícekrát ?
Případně nějaký Fix ?
Děkuji mnohokrát, klidně doplním případné info.
