C# 我必须中止这个线程吗?正在等待命名管道。我如何正确地做到这一点?

C# 我必须中止这个线程吗?正在等待命名管道。我如何正确地做到这一点?,c#,.net,multithreading,named-pipes,termination,C#,.net,Multithreading,Named Pipes,Termination,但在这里,我有一个问题,优雅地终止我的应用程序。我的主要代码如下。有两个问题。1) 我正在使用Thread.Abort和2)此应用程序实际上并没有结束。我可以设置一个断点,然后看到调用了abort,并转到了结束括号,但是IDE仍然处于调试模式,并且进程仍然处于活动状态(在process manager中)。我如何正确地终止此操作 [STAThread] static void Main(string[] args) { Thread t; t = new Thread(new T

但在这里,我有一个问题,优雅地终止我的应用程序。我的主要代码如下。有两个问题。1) 我正在使用Thread.Abort和2)此应用程序实际上并没有结束。我可以设置一个断点,然后看到调用了abort,并转到了结束括号,但是IDE仍然处于调试模式,并且进程仍然处于活动状态(在process manager中)。我如何正确地终止此操作

[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");
}

关于线程。从MS文档中中止。。。“调用此方法通常会终止线程。” 此外,“线程不能保证立即中止,或者根本不能中止。”


我怀疑WaitForConnection正在阻止它接收线程中止。一般来说,线程中止被认为是邪恶的,因为谁知道你可以将事情保留在什么状态,等等。请参阅此处以获得更多帮助…

正如你所建议的那样。。。不要使用Thread.Abort。除非你有一个非常令人信服的理由,为什么没有其他选择会起作用,否则这是一个坏主意

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

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

当ThreadStartServer方法完成其工作后,您需要从该方法“返回”。如果将其与Main方法中的Join()结合使用,工作线程将顺利完成。另外,将其设置为背景线程。以下是一个示例(不含管道流):

class程序
{
静态void Main(字符串[]参数)
{
螺纹t;
t=新线程(新线程开始(ThreadStartServer));
t、 IsBackground=true;
尝试
{
t、 Start();
//这里的工作很费时
}
捕获(System.IO.IOException)
{
//从你的例子
}
t、 Join();
}
静态公共void ThreadStartServer()
{
while(true)
{
int计数器=0;
而(++计数器<10)
{
控制台。写线(“工作”);
//做耗时的事情
睡眠(500);
}
返回;
} 
}
}

实际上不是,问题是pipeStream。WaitForConnection()阻塞了。这个问题用它来解释,所以它没有帮助。你当然有权。。。我读得不够仔细。为什么不考虑使用BeginWaitForConnection,以避免在那里使用阻塞调用。问题在于阻塞WaitForConnection()。不是如何从线程返回。如果删除Join()并使其成为工作线程背景,则当主线程终止时,它将正常退出,
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;

        } 
    }
}