0
不太清楚如何做到這一點,或者甚至是編程c#的正確方法。也許我需要重新思考我正在嘗試做什麼,但我需要一些幫助。C#重新調用當前方法
我有一個wpf應用程序,用於啓動/停止服務,並使用命名管道打開端口並與服務進行通信。所以當服務需要時它可以發送消息給應用程序。我啓動了一個新線程來調用位於namedPipeServerStream.WaitForConnection()中的Wait()方法。這工作正常,但當服務停止時,我發送消息到應用程序,所以它打破了WaitForConnection,但我不想殺死這個線程,我想重新激活相同的方法,並等待一個循環,直到服務再次啓動。不確定最好的方法來做到這一點。
我到目前爲止的代碼如下。
void Wait()
{
while (!ServiceControl.ServiceRunning)
{
Thread.Sleep(250);
if (exitProgram == true)
{
break;
}
}
while (ServiceControl.ServiceRunning)
{
try
{
NamedPipeServerStream namedPipeServerStream = new NamedPipeServerStream("pipeName");
namedPipeServerStream.WaitForConnection();
byte[] buffer = new byte[255];
namedPipeServerStream.Read(buffer, 0, 255);
string request = ASCIIEncoding.ASCII.GetString(buffer);
if (request != null)
{
if (request == "pipeExit")
{
Wait(); //Reinvoke Wait if svc calls to exit
}
else
{
//Do work on message
}
}
namedPipeServerStream.Close();
}
catch (Exception){}
}
if (_opener.exitProgram == true)
{
Application.Current.Dispatcher.InvokeShutdown();
}
哈哈,那很容易。謝謝! – Ron 2012-02-28 17:09:54