0

我对相同的代码还有另一个问题,并在客户端关闭它后保持管道打开

但是在这里我有一个问题优雅地终止我的应用程序。我的主要代码如下。有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");
}
4

3 回答 3

2

关于 MS 文档中的 Thread.Abort ...“调用此方法通常会终止线程。” 此外,“不能保证线程立即或完全中止。”

我怀疑 WaitForConnection 阻止它接收线程中止。一般来说,线程中止被认为是邪恶的,因为谁知道你可以把东西留在什么状态等等。请参阅这里以获得更多帮助...... http://www.interact-sw.co.uk/iangblog/2004/11/ 12/取消

于 2009-11-06T20:49:41.680 回答
0

正如你所建议的......不要使用 Thread.Abort。除非您有一个非常令人信服的理由说明没有其他选择可行,否则这是一个坏主意。

问题是对 ReadLine 的阻塞调用......所以改为使用 StreamReader.Peek/Read 从命名管道中提取数据。这将允许您检查循环中的标志,以便您可以退出。

对于更复杂的解决方案,您可以使用异步 I/O ...有关一些指针,请参阅此问题

于 2009-11-06T21:00:30.753 回答
0

完成工作后,您需要从 ThreadStartServer 方法“返回”。如果将此与 Main 方法中的 Join() 结合使用,工作线程将优雅地完成。另外使其成为背景线程。这是一个示例(没有 PipeStream):

class Prog
{
    static void Main(string[] args)
    {
        Thread t;
        t = new Thread(new ThreadStart(ThreadStartServer));
        t.IsBackground = true;
        try
        {
            t.Start();

            // time consuming work here
        }
        catch (System.IO.IOException)
        {
            // from your example
        }

        t.Join();
    }
    static public void ThreadStartServer()
    {
        while (true)
        {
            int counter=0;
            while (++counter < 10)
            {
                Console.WriteLine("working.");
                // do time consuming things
                Thread.Sleep(500);

            }
            return;

        } 
    }
}
于 2009-11-06T22:43:12.930 回答