与 StreamWriter 异步使用命名管道
我正在尝试使用 StreamWriter 通过命名管道发送字符串,但 StreamWriter 类仅提供同步操作。我可以使用 NamedPipeServerStream 类的 BeginWrite 方法,但我想知道为什么没有允许异步操作的编写器类。我错过了一些明显的东西吗?
I am trying to send a string over a named pipe using StreamWriter, but StreamWriter class only offers synchronous operations. I can use BeginWrite method of the NamedPipeServerStream class, but I wonder why there are no writer classes that would allow asynchronous operations. Am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它会比原始流复杂得多。对于原始流,任何数量的数据都可能异步传入,系统只是将缓冲区传递给您。阅读器需要字符编码,可以将几个字节的原始数据转换为单个 Unicode 字符。这并不是不可能的,框架库只是没有做到这一点,所以您需要自己完成这项工作。
(根据您的需要,创建另一个线程并在其上同步执行操作可能会使编写程序变得更容易。请注意,当您使用 Begin/End 异步时,可伸缩性通常会更高。)
It would be significantly more complicated than for the raw streams. For the raw streams, any amount of data might come in asynchronously and the system just passes the buffer to you. The reader requires character encoding which may turn several bytes of raw data into a single Unicode character. Not that this would be impossible, the framework libraries just don't take it that far so you'll need to do this work yourself.
(Depending on your needs, creating another thread and performing the operations synchronously on it might make it easier to write your program. Note that scalability would be generally be higher when you use Begin/End async.)