1

I am looking for execute an external program inside a .net application.

I need to assign a concrete processor to that program. I need to execute the program several times and every time on a different processor. I need to wait for external program termination.

To simplify I have done a small spike (console) app to work on it:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

    namespace ExecuteExternalInParallelWithAffinity
    {
        class Program
        {
            static void Main(string[] args)
            {
                for (int i = 1; i < 3; i++)
                {
                    var processnumber = i;
                    Task.Factory.StartNew(() =>
                        {                        
                            Process p = Process.Start("notepad.exe");                       
                            p.ProcessorAffinity = (IntPtr)processnumber;
                        }).ContinueWith(t => 
                        {
                            Console.Out.WriteLine(string.Format("Process ended on processor: {0}", processnumber));
                        });
                }
                Console.ReadKey();
            }
        }
    }

My questions:
1.- Can I assign processor affinity before executing the process.
2.- In spite I am checkin Notepad on task manager, all of them are assigned to processor 0.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
ferpega
  • 3,182
  • 7
  • 45
  • 65
  • possible duplicate of [Wait till a process ends](http://stackoverflow.com/questions/3147911/wait-till-a-process-ends) – Erik Philips Jun 19 '14 at 18:31
  • There is not a good reason to start a process by another thread. – Erik Philips Jun 19 '14 at 18:32
  • @ErikPhilips I have found how to wait for process ends, but still need to assign a process to concrete processor. And, sorry I don't understand your second comment. – ferpega Jun 19 '14 at 18:42
  • `Task.Factory.StartNew()` is spinning up a thread. It appears that threads *only* use is to start a process. This provides no benefit what so ever. – Erik Philips Jun 19 '14 at 18:48
  • @ErikPhilips if do you have any other better solution to execute an external process assigned to a concrete processor, I will be glad to learn from it. Thanks. – ferpega Jun 19 '14 at 18:52
  • 1
    What does the thread have to do with which processor the external executable runs on? Those are completely independent of each other. The process starts and executes on it's own thread independent of the thread that started the process. – Erik Philips Jun 19 '14 at 18:54

1 Answers1

0

The Process class does not support setting certain settings before the new process starts. The native CreateProcess API does allow that, though. You'll have to use that one.

Start the process in a suspended state. Set all the settings you like. Then, resume it. That way all code in that process will run under the new settings.

usr
  • 168,620
  • 35
  • 240
  • 369
  • Thanks @usr I am now looking for for an example on CreateProcess in spite I would not like to go out managed code. – ferpega Jun 19 '14 at 21:13
  • You'll find such an example with your favorite search engine. – usr Jun 19 '14 at 21:15