2012-10-05 93 views
0

大家好我創建了一個基於服務的應用程序,它與管道通信 服務應用程序一直運行,直到windows應用程序停止,其中包含 管道服務器代碼。保持應用程序運行

+6

請不要在標題中加入標籤。相反,使用標記系統。 –

回答

0

請嘗試下面的代碼。您必須將代碼中的此過程與您已有的服務代碼進行合併。將「PipeServiceName.exe」替換爲調用該進程的名稱。此外,此代碼每5秒檢查一次。您可以通過更改5000號碼來改變這一點。

不知道更多關於「管道」和服務如何相互作用,很難把工作流程放在一起。

private readonly ManualResetEvent _shutdownEvent = new ManualResetEvent(false); 
private Thread _thread; 

public MyService() 
{ 
    InitializeComponent(); 
} 

protected override void OnStart(string[] args) 
{ 
    _thread = new Thread(MonitorThread) 
    { 
     IsBackground = true 
    } 
} 

protected override void OnStop() 
{ 
    _shutdownEvent.Set(); 
    if (!_thread.Join(5000)) 
    { 
     _thread.Abort(); 
    } 
} 

private void MonitorThread() 
{ 
    while (!_shutdownEvent.WaitOne(5000)) 
    { 
     Process[] pname = Process.GetProcessesByName("PipeServiceName.exe"); 
     if (pname.Count == 0) 
     { 
      // Process has stopped. ReLaunch 
      RelaunchProcess(); 
     } 
    } 
} 

private void RelaunchProcess() 
{ 
    Process p = new Process(); 

    p.StartInfo.FileName = "PipeServiceName.exe"; 
    p.StartInfo.Arguments = ""; // Add Arguments if you need them 

    p.Start(); 
} 
相關問題