c# - 当我坐在 Console.ReadLine() 上时,委托(delegate)会运行吗?

标签 c# .net delegates

我对委托(delegate)非常陌生!

我有一个运行方法的 AsyncCallback 委托(delegate)。

此方法定期将文本写入控制台 (Console.WriteLine("FooBar"))

此委托(delegate)从我的 Main 方法启动,我需要找到一种方法在委托(delegate)运行时保持此 Main 方法打开。否则,程序将启动、启动委托(delegate)并再次关闭,所以我正在使用 Console.Readline。

这行得通吗?我的程序是否能够同时坐在 Console.ReadLine 上,而我的委托(delegate)定期使用 Console.WriteLine 将文本写入控制台,还是我是个白痴?我的代码如下:

static void Main(string[] args)
{
    NetworkStream myNetworkStream;
    Socket socket;
    IPEndPoint maxPort = new IPEndPoint(IPAddress.Parse("x.x.x.x"), xxxx);

    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
    socket.Connect(maxPort);

    myNetworkStream = new NetworkStream(socket);

    byte[] buffer = new byte[1024];
    int offset = 0;
    int count = 1024;

    string loginString = "FOOBARR";
    ASCIIEncoding encoder = new ASCIIEncoding();

    myNetworkStream.BeginRead(buffer, offset, count, new AsyncCallback(OnBeginRead), myNetworkStream);
    myNetworkStream.Write(encoder.GetBytes(loginString), 0, encoder.GetByteCount(loginString));

    Console.ReadLine();
}

public static void OnBeginRead(IAsyncResult ar)
{
    NetworkStream ns = (NetworkStream)ar.AsyncState;
    int bufferSize = 2014;
    byte[] received = new byte[bufferSize];
    string result = String.Empty;

    ns.EndRead(ar);

    int read;

    while (true)
    {
        if (ns.DataAvailable)
        {
            read = ns.Read(received, 0, bufferSize);
            result += Encoding.ASCII.GetString(received);
            received = new byte[bufferSize];

            Console.WriteLine(result);
        }
        else
        {
            Thread.Sleep(1000);
        }
    }
}

此外,如果我使用不同的参数多次调用“myNetworkStream.BeginRead”,我的“OnBeginRead”方法的不同版本是否会在处理器上的单独线程上每次启动,或者正在运行的方法是否会停止并被替换是最近的那个吗?

最佳答案

Will my program be able to both sit at Console.ReadLine while my delegate periodically writes >text to the console with Console.WriteLine?

简短回答:是

if I call 'myNetworkStream.BeginRead' multiple times with different parameters will a different version of my 'OnBeginRead' method start each time on a seperate thread on the processor or will the method that was running stop and be replace by the more recent one

MSDN 说多次调用 BeginRead

Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

这意味着您不应从多个线程调用 BeginRead。这将导致意想不到的行为

关于c# - 当我坐在 Console.ReadLine() 上时,委托(delegate)会运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8992080/

相关文章:

c# - 在不同线程上访问 WebBrowser

c# - 使用异步等待时出现意外结果

c# - 使用 RichTextBox 在文本追加中创建延迟

c# - 迁移到 .NET 4.51 时 SSLStream 抛出异常

c# - 是否可以将委托(delegate)作为属性参数?

c# - 如何创建更精准的搜索?

java - 设置日志文件名以在 Log4j 中包含当前日期

.net - 无法运行 RegAsm : Error related to access to the Registry

c# - 如何正确注销事件处理程序

c# - .NET 4.0 中的协变和逆变错误