Restart low-priority service on the third core in C#

Asked

Viewed 61 times

1

I have a job running on the machine in production. This service sometimes gets "stopped" and the easiest solution is to restart the service, and it continues to function as expected. Is there any way I can get this service in a C# and restart it? What’s more, if you can restart it, can you change the service priority and affinity? I need it to run at low priority and only on the third core of the processor. Thank you

  • you may also choose the windows command line: https://superuser.com/questions/620724/changing-windows-process-priority-via-command-line

  • see: https://stackoverflow.com/a/4219923/4713574 and https://msdn.microsoft.com/en-us/library/system.diagnostics.processthread.processoraffinity.aspx?f=255&MSPPError=-2147217396

2 answers

1

You can try using namespace System.Diagnostics to do the reboot:

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

And to change the process priority, use:

foreach(Process proc in Process.GetProcessesByName("nome do processo"))
{
    proc.PriorityClass = ProcessPriorityClass.BelowNormal;
}
  • as to affinity, I saw that using processo.ProcessorAffinity = (IntPtr)1; I’ll keep the process running at one core, but this way it’s only the first one that’s activated, I need it to be the last one. How can I specify this for the ProcessorAffinity?

  • @ihavenokia Already tried to put 3 instead of 1?

  • of course, I even put 8 (because here on my machine I have more than on the machine where I need the program), I thought the program did nothing, but then I realized that it selects the core Nr that I put, and I needed the number 1, but the position was 3 (the index should be 2)

  • @ihavenokia Try to use this: proc.IdealProcessor = 3;

  • did not work. When I go to task manager it is at the core [0]. And the IdealProcessor is a Thread method, not a process method. Or it will be that over time the program will run at core 3?

  • @ihavenokia I don’t think so. I really have to admit that I don’t know how to help you in this of changing the core.

Show 1 more comment

0


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

     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 for switching 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.