.NET中是否有阻塞的StreamReader,TextReader或StringReader? [英] Is there a blocking StreamReader, TextReader, or StringReader in .NET?

查看:86
本文介绍了.NET中是否有阻塞的StreamReader,TextReader或StringReader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个后台任务,该任务从TextReader读取输入并一次处理一行.我希望后台任务能够阻止,直到用户在字段中键入一些文本并单击提交"按钮为止.是否有某种TextReader风格会阻塞,直到文本可用,并让您以某种方式将更多文本添加到基础源中?

I want to run a background task that reads input from a TextReader and processes it a line at a time. I want the background task to block until the user types some text into a field and clicks the submit button. Is there some flavour of TextReader that will block until text is available and lets you somehow add more text to the underlying source?

我认为指向同一MemoryStream的StreamReader和StreamWriter可能有效,但似乎不起作用. StreamReader看到MemoryStream在开始时为空,从此不再检查.

I thought that a StreamReader and StreamWriter pointing to the same MemoryStream might work, but it doesn't seem to. The StreamReader sees that the MemoryStream is empty at the start, and never checks again.

我意识到编写一个ProcessLine()方法并在用户单击提交"按钮时调用它会更容易.但是,我正在尝试设计一个插件体系结构,我希望插件看起来像带有输入流和输出流的老式控制台应用程序.我希望插件的输入流一直阻塞,直到用户单击带有一些输入文本的提交"按钮为止.

I realize that it would be easier to write a ProcessLine() method and call it whenever the user clicks the submit button. However, I'm trying to design a plug-in architecture, and I'd like the plug ins to look like old-fashioned console apps with an input stream and an output stream. I want the plug in's input stream to just block until the user clicks the submit button with some input text.

推荐答案

似乎没有此实现-很奇怪,因为我同意这将是一个有用的构造.但是,编写起来应该很简单.这样的事情应该起作用:

It seems that there is no implementation of this - which is strange, since I agree that it would be a useful construct. But it should be simple to write. Something like this should work:

  public class BlockingStream: Stream
  {
    private readonly Stream _stream;

    public BlockingStream(Stream stream)
    {
      if(!stream.CanSeek)
        throw new ArgumentException("Stream must support seek", "stream");
      _stream = stream;
    }

    public override void Flush()
    {
      lock (_stream)
      {
        _stream.Flush();
        Monitor.Pulse(_stream);
      }
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
      lock (_stream)
      {
        long res = _stream.Seek(offset, origin);
        Monitor.Pulse(_stream);
        return res;
      }
    }

    public override void SetLength(long value)
    {
      lock (_stream)
      {
        _stream.SetLength(value);
        Monitor.Pulse(_stream);
      }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
      lock (_stream)
      {
        do
        {
          int read = _stream.Read(buffer, offset, count);
          if (read > 0)
            return read;
          Monitor.Wait(_stream);
        } while (true);
      }
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
      lock (_stream)
      {
        long currentPosition = _stream.Position;
        _stream.Position = _stream.Length;
        _stream.Write(buffer, offset, count);
        _stream.Position = currentPosition;
        Monitor.Pulse(_stream);
      }
    }

    public override bool CanRead
    {
      get
      {
        lock (_stream)
        {
          return _stream.CanRead;
        }
      }
    }

    public override bool CanSeek
    {
      get
      {
        lock (_stream)
        {
          return _stream.CanSeek;
        }
      }
    }

    public override bool CanWrite
    {
      get
      {
        lock (_stream)
        {
          return _stream.CanWrite;
        }
      }
    }

    public override long Length
    {
      get
      {
        lock (_stream)
        {
          return _stream.Length;
        }
      }
    }

    public override long Position
    {
      get
      {
        lock (_stream)
        {
          return _stream.Position;
        }
      }
      set
      {
        lock (_stream)
        {
          _stream.Position = value;
          Monitor.Pulse(_stream);
        }
      }
    }
  }

这篇关于.NET中是否有阻塞的StreamReader,TextReader或StringReader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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