2012-10-05 90 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(); 
} 
相关问题