この同じコードについて別の質問があり、クライアントがパイプを閉じた後もパイプを開いたままにします
しかし、ここでアプリを正常に終了する際に問題があります。私の主なコードは以下です。2つの問題があります。1) Thread.Abort を使用しており、2) このアプリケーションは実際には終了しません。ブレークポイントを設定すると、abort が呼び出されて終了ブレースにステップするのを確認できますが、IDE はまだデバッグ モードであり、プロセスは (プロセス マネージャーで) まだ生きています。これを適切に終了するにはどうすればよいですか?
[STAThread]
static void Main(string[] args)
{
Thread t;
t = new Thread(new ThreadStart(ThreadStartServer));
bool hasInstance = true;
try
{
pipeStream = new NamedPipeServerStream(pipename);
hasInstance = false;
pipeStream.Close();
t.Start();
pipeStream.Dispose();
}
catch (System.IO.IOException)
{
hasInstance = true;
}
if (hasInstance)
{
clientPipeMessage(args[1]);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
t.Abort();
}
static public void ThreadStartServer()
{
while (true)
{
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream(pipename))
{
Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode());
// Wait for a connection
pipeStream.WaitForConnection();
Console.WriteLine("[Server] Pipe connection established");
using (StreamReader sr = new StreamReader(pipeStream))
{
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("{0}: {1}", DateTime.Now, temp);
}
}
}
}
Console.WriteLine("Connection lost");
}