2010-11-15 46 views
5

我正在启动公开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!! 

回答

3

我会使用系统范围内的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方法之一进行设置。