public static ProcDomain CreateDomain(string name, string executablePath, bool runElevated) { ProcDomain domain = new ProcDomain(); domain._pipeName = name + "_" + Guid.NewGuid().ToString(); //create a named pipe //the child process will connect as a client but both sides act as a server and a client sending and waiting for messages var pipeServer = new NamedPipeServerStream(domain._pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous) { ReadMode = PipeTransmissionMode.Message }; domain._pipe = pipeServer; //start waiting for a connection before creating the process #if net45 Task clientConnected = Task.Factory.StartNew(() => pipeServer.WaitForConnection()); #else Task clientConnected = pipeServer.WaitForConnectionAsync(); // new CancellationTokenSource(2000).Token); #endif //create the process var proc = domain.CreateDomainProcess(executablePath, runElevated); //this will unwind any exception comming from WaitForConnection clientConnected.GetAwaiter().GetResult(); //start listening for communications from the client Task throwaway = domain.ListenToChildDomainAsync(); return(domain); }
public static void HostDomain(string pipeName) { ProcDomain.InitializeCurrentDomain(pipeName); ManualResetEventSlim unloaded = new ManualResetEventSlim(false); ProcDomain.GetCurrentProcDomain().Unloaded += p => unloaded.Set(); unloaded.Wait(); }
internal static void InitializeCurrentDomain(string pipeName = null) { lock (s_currentDomainLock) { if (s_currentDomain == null) { s_currentDomain = new ProcDomain(); s_currentDomain._pipeName = pipeName; //if the pipe name is specified this is a child domain if (s_currentDomain._pipeName != null) { s_currentDomain.InitializeChildDomain(); } } } }