2010-11-15 42 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方法之一進行設置。