Signalling to a parent process that a child process is fully initialised

浪尽此生 提交于 2019-12-08 21:03:16

问题


I'm launching a child process that exposes a WCF endpoint. How can I signal from the child process to the parent process that the child is fully initialised and that it can now access the endpoint?

I'd thought about using Semaphores for this purpose but can't quite figure out how to achieve the required signal.

        string pipeUri = "net.pipe://localhost/Node0";
        ProcessStartInfo startInfo = new ProcessStartInfo("Node.exe", "-uri=" + pipeUri);
        Process p = Process.Start(startInfo);
        NetNamedPipeBinding binding = new NetNamedPipeBinding();
        var channelFactory = new ChannelFactory<INodeController>(binding);
        INodeController controller = channelFactory.CreateChannel(new EndpointAddress(pipeUri));

        // need some form of signal here to avoid..
        controller.Ping() // EndpointNotFoundException!!

回答1:


I would use a system-wide EventWaitHandle for this. The parent application can then wait for the child process to signal that event.

Both processes create the named event and then one waits for it to be signalled.

// I'd probably use a GUID for the system-wide name to
// ensure uniqueness. Just make sure both the parent
// and child process know the GUID.
var handle = new EventWaitHandle(
    false,
    EventResetMode.AutoReset,
    "MySystemWideUniqueName");

While the child process will just signal the event by calling handle.Set(), the parent process waits for it to be set using one of the WaitOne methods.



来源:https://stackoverflow.com/questions/4187020/signalling-to-a-parent-process-that-a-child-process-is-fully-initialised

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!