我正在启动一个公开 WCF 端点的子进程。我如何从子进程向父进程发出信号,表明子进程已完全初始化并且现在可以访问端点?
我考虑过为此目的使用信号量,但不太清楚如何获得所需的信号。
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!!
最佳答案
我会使用系统范围的 EventWaitHandle
为了这。然后父应用程序可以等待子进程发出该事件的信号。
两个进程都创建指定的事件,然后一个等待它收到信号。
// 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");
虽然子进程将通过调用 handle.Set()
发出事件信号,但父进程使用 WaitOne
方法之一等待它被设置。
关于c# - 向父进程发出子进程已完全初始化的信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4187020/