我想启动2个相关的应用程序。首先,我想启动我的“服务”应用程序,然后启动我的“客户端”应用程序。它并不能始终如一地发挥作用。有时客户端启动太快而最终无法连接到服务。有人可以告诉我哪里需要更改代码才能正常工作并且只有在服务完全启动后才启动客户端吗?
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Starting Service");
StartService();
if (IsServiceRunning())
{
Console.WriteLine("Starting Client");
StartClient();
}
Console.ReadLine();
}
private static void StartClient()
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
WorkingDirectory = @"C:\Client",
FileName = "Client.exe"
};
Process.Start(startInfo);
}
private static bool IsServiceRunning()
{
Console.WriteLine("Check to see is running...");
Process[] pname = Process.GetProcessesByName("MyCommonService");
int runningCheck = 0;
if (pname.Length == 0 || runningCheck < 10)
{
Console.WriteLine("Did not find the process. Check again...");
runningCheck += 1;
Thread.Sleep(250);
IsServiceRunning();
}
Thread.Sleep(1000);
return true;
}
private static void StartService()
{
Console.WriteLine("Starting Service");
ProcessStartInfo startInfo = new ProcessStartInfo()
{
WorkingDirectory = @"C:\Service",
FileName = "MyCommonService.exe"
};
Process.Start(startInfo);
}
}
慕哥6287543
相关分类