admin管理员组

文章数量:1026989

I have a Windows service that is supposed to restart itself after a specified time using a method called ServiceRestartTimer. I'm installing this service on Windows Server 2019.

When the service reaches the Stop state, it seems to stop completely, and the rest of the code doesn't execute anymore.

Based on the logs I've added, I realized this is what's happening.

This is my code:

if (AppConfiguration.RestartTimeMin != 0)
{
    RestartTimeer.Elapsed += ServiceRestartTimer;
    RestartTimeer.Start();
}

In this section, which runs after the OnStart method, I call the ServiceRestartTimer method, which goes to the following method:

private void ServiceRestartTimer(object sender, ElapsedEventArgs e)
{
    try
    {
        DateTime now = DateTime.Now;
        TimeSpan diff = now - LatestReadTime;

        Log.WriteToFile("Service Entered ServiceRestartTimer at: " + now.ToString());

        // Checking if the delay exceeds the allowed time
        if (diff.TotalSeconds > RestartTimeInSec)
        {
            Ping myPing = new Ping();

            // Executing Ping asynchronously
            myPing.PingCompleted += (pingSender, pingEventArgs) =>
            {
                try
                {
                    PingReply reply = pingEventArgs.Reply;

                    if (reply.Status == IPStatus.Success)
                    {
                        Log.WriteToFile("ReStart Service Is Called: " + " In Time : " + DateTime.Now.ToString());

                        try
                        {
                            // Using ServiceController to control the service
                            ServiceController sc = new ServiceController("MyService");

                            // Checking the service status before stopping
                            if (sc.Status == ServiceControllerStatus.Running)
                            {
                                Log.WriteToFile($"Service MyService is currently running. Attempting to stop it.");
                                sc.Stop();
                                sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(15)); // Wait for the service to stop
                                Log.WriteToFile($"Service MyService stopped successfully.");
                            }

                            // Restarting the service after stopping
                            sc.Start();
                            sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(15)); // Wait for the service to start
                            Log.WriteToFile($"Service MyService started successfully.");
                        }
                        catch (Exception ex)
                        {
                            Log.WriteToFile($"Error restarting service: {ex.Message}");
                        }
                    }
                    else
                    {
                        Log.WriteToFile("Ping failed. Service restart skipped.");
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteToFile("Exception in PingCompleted handler: " + ex.Message);
                }
            };

            // Sending Ping asynchronously
            myPing.SendAsync(AppConfiguration.IP, 1000, null);
        }
    }
    catch (Exception ex)
    {
        Log.WriteToFile("Exception in ServiceRestartTimer: " + ex.Message);
    }
}

This log:

Log.WriteToFile("ReStart Service Is Called: " + " In Time : " + DateTime.Now.ToString());

is printed, and so is this log:

Log.WriteToFile($"Service MyService is currently running. Attempting to stop it.");

But when sc.Stop(); is executed, the next log:

Log.WriteToFile($"Service MyService stopped successfully.");

is not printed, and it seems like the service stops completely, and the rest of the code doesn't run anymore.

I expect that when this method is reached, it should correctly perform the service restart operation.

I have tested various implementations, but none of them worked.

I have a Windows service that is supposed to restart itself after a specified time using a method called ServiceRestartTimer. I'm installing this service on Windows Server 2019.

When the service reaches the Stop state, it seems to stop completely, and the rest of the code doesn't execute anymore.

Based on the logs I've added, I realized this is what's happening.

This is my code:

if (AppConfiguration.RestartTimeMin != 0)
{
    RestartTimeer.Elapsed += ServiceRestartTimer;
    RestartTimeer.Start();
}

In this section, which runs after the OnStart method, I call the ServiceRestartTimer method, which goes to the following method:

private void ServiceRestartTimer(object sender, ElapsedEventArgs e)
{
    try
    {
        DateTime now = DateTime.Now;
        TimeSpan diff = now - LatestReadTime;

        Log.WriteToFile("Service Entered ServiceRestartTimer at: " + now.ToString());

        // Checking if the delay exceeds the allowed time
        if (diff.TotalSeconds > RestartTimeInSec)
        {
            Ping myPing = new Ping();

            // Executing Ping asynchronously
            myPing.PingCompleted += (pingSender, pingEventArgs) =>
            {
                try
                {
                    PingReply reply = pingEventArgs.Reply;

                    if (reply.Status == IPStatus.Success)
                    {
                        Log.WriteToFile("ReStart Service Is Called: " + " In Time : " + DateTime.Now.ToString());

                        try
                        {
                            // Using ServiceController to control the service
                            ServiceController sc = new ServiceController("MyService");

                            // Checking the service status before stopping
                            if (sc.Status == ServiceControllerStatus.Running)
                            {
                                Log.WriteToFile($"Service MyService is currently running. Attempting to stop it.");
                                sc.Stop();
                                sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(15)); // Wait for the service to stop
                                Log.WriteToFile($"Service MyService stopped successfully.");
                            }

                            // Restarting the service after stopping
                            sc.Start();
                            sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(15)); // Wait for the service to start
                            Log.WriteToFile($"Service MyService started successfully.");
                        }
                        catch (Exception ex)
                        {
                            Log.WriteToFile($"Error restarting service: {ex.Message}");
                        }
                    }
                    else
                    {
                        Log.WriteToFile("Ping failed. Service restart skipped.");
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteToFile("Exception in PingCompleted handler: " + ex.Message);
                }
            };

            // Sending Ping asynchronously
            myPing.SendAsync(AppConfiguration.IP, 1000, null);
        }
    }
    catch (Exception ex)
    {
        Log.WriteToFile("Exception in ServiceRestartTimer: " + ex.Message);
    }
}

This log:

Log.WriteToFile("ReStart Service Is Called: " + " In Time : " + DateTime.Now.ToString());

is printed, and so is this log:

Log.WriteToFile($"Service MyService is currently running. Attempting to stop it.");

But when sc.Stop(); is executed, the next log:

Log.WriteToFile($"Service MyService stopped successfully.");

is not printed, and it seems like the service stops completely, and the rest of the code doesn't run anymore.

I expect that when this method is reached, it should correctly perform the service restart operation.

I have tested various implementations, but none of them worked.

本文标签: The issue of a Windows service not restarting using C Stack Overflow