为什么在提示文字时C#控制台应用程序会停止运行? [英] Why Does C# Console Application Stop Running When Text is HIghlighted?

查看:76
本文介绍了为什么在提示文字时C#控制台应用程序会停止运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,它充当同步服务器套接字,以从同一台计算机上的同步客户端套接字接收数据.

I have a console application which acts as a Synchronous Server Socket to receive data from a Synchronous Client Socket on the same machine.

从客户端向服务器发送几次数据后,我意识到,如果要在控制台中突出显示某些文本,它将停止运行代码,并且客户端将无法建立连接.

After sending some data a few times from the Client to the Server I realize that if I were to highlight some text in the console, it would stop running code and the Client would not be able to establish a connection.

我想知道发生这种情况的原因,以及是否有办法使代码仍然运行.

I'd like to know the reason why this happens and if there is a way to make the code still run.

推荐答案

基本上,控制台被锁定,以防止其不得不缓冲可能无限量的数据(对于执行大量任务的程序控制台IO).您可以在这里看到它:

Essentially, the console is locked, to prevent it having to buffer a possibly infinite amount of data (for programs that do a lot of console IO). You can see this here:

using System;
using System.Net;
using System.Text;
using System.Threading;

static class P {
    static void Main() {
        using (var http = new HttpListener())
        {
            http.Prefixes.Add("http://localhost:20000/");
            http.Start();

            ThreadPool.QueueUserWorkItem(delegate {
                try {
                    while (true) {
                        var ctx = http.GetContext();
                        Console.Write("."); // <===== this
                        var buffer = Encoding.UTF8.GetBytes(DateTime.Now.ToString());
                        ctx.Response.OutputStream.Write(buffer, 0, buffer.Length);
                        ctx.Response.OutputStream.Close();
                    }
                }
                catch { }
            });

            Console.WriteLine("Running for 5 minutes...");
            Thread.Sleep(TimeSpan.FromMinutes(5));
            http.Stop();
        }
    }
}

如果运行此命令,它将设置一个Web服务器,当您点击 http://localhost:时,该服务器将只写入当前时间:20000/.在当前代码中,如果您在控制台上选择一个区域,它将中断网络服务器,因为网络服务器在以下位置访问控制台(不是一个好主意)这行:

If you run this, it sets up a web-server that just writes the current time when you hit http://localhost:20000/. In the current code, it will interrupt the web-server if you select an area on the console, because the web-server accesses the console (not a good idea) in this line:

Console.Write("."); // <===== this

现在,请:删除(或注释掉)该行,然后重试.现在,当您在屏幕上选择文本时,它不会停止网络服务器,因为该网络服务器不会尝试访问控制台.

So now: remove (or comment out) that line and retry. Now, it doesn't stop the web-server when you select text on the screen, because the web-server doesn't attempt to access the console.

因此回答您的问题:查看以任何方式访问控制台的任何代码.通常,这可以在保留输出的同时,通过将主代码写入某种队列来完成(从字面上看, Queue< string> 会很好),并且具有 different (非关键)线程执行从队列到控制台的写入.如果输出队列被阻塞:没人在乎.

So to answer your question: look at any code that accesses the console in any way. This can usually be done while retaining outputs by having the main code write to some kind of queue (literally a Queue<string> would be fine), and have a different (non-critical) thread do the writing from the queue to the console. If the output queue gets blocked: no-one cares.

这篇关于为什么在提示文字时C#控制台应用程序会停止运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆