Restart service at low priority in the third core in C #

1

I have a service running on the machine in production. This service sometimes gets "stopped" and the easiest solution is to restart the service, and it continues to work as expected. Is there any way I can get this service in a program in C# and restart it? Furthermore, if it is possible to restart, is it possible to change the priority and affinity of the service? I need it to run at low priority and only on the third core of the processor. Thanks

    
asked by anonymous 17.08.2017 / 12:46

2 answers

0

After some research I was able to do what I wanted. This code picks up a process, changes the priority to the lowest, and leaves the affinity for only the third CPU core

     Process[] services = Process.GetProcessesByName("WindowsBusinessService");

            try
            {
                //change priority
                services[0].PriorityClass = ProcessPriorityClass.Idle;

                //change affinity
                services[0].ProcessorAffinity = (IntPtr)0x0008;
            }
            catch (Exception)
            {

                Console.WriteLine("ERROR CHANGING PROCESS");
            }

A list of codes to change cores:

       0x1 - 0001 - Core0
       0x2 - 0010 - Core1
       0x3 - 0011 - Core1 & Core0
       0x4 - 0100 - Core2
       0x5 - 0101 - Core2 & Core0
       0x6 - 0110 - Core2 & Core1
       0x7 - 0111 - Core2 & Core1 & Core0
       0x8 - 1000 - Core3
       0x9 - 1001 - Core3 & Core0
       0xA - 1010 - Core3 & Core1
       0xB - 1011 - Core3 & Core1 & Core0
       0xC - 1100 - Core3 & Core2
       0xD - 1101 - Core3 & Core2 & Core0
       0xE - 1110 - Core3 & Core2 & Core1
       0xF - 1111 - Core3 & Core2 & Core1 & Core0
    
21.08.2017 / 15:26
1

You can try to use the System.Diagnostics namespace to reboot:

foreach(Process proc in Process.GetProcessesByName("nome do processo"))
{
    proc.Kill();
}
Process.Start(@"diretório do serviço");

And to change process priority, use:

foreach(Process proc in Process.GetProcessesByName("nome do processo"))
{
    proc.PriorityClass = ProcessPriorityClass.BelowNormal;
}
    
17.08.2017 / 13:08