[英]C# chaining methods using streams
I have two different objects in c# that expose methods using streams. 我在c#中有两个不同的对象,它们使用流来公开方法。
One of them exposes a method that takes a Stream as a parameter and writes to the stream, while the other exposes a method that takes a Stream as a parameter and reads from it. 其中一个公开了一个方法,该方法将Stream作为参数并写入流,而另一个公开了一个方法,该方法将Stream作为参数并从中读取。
Writing stuff to a MemoryStream is out of the question as there is too much data to keep completely in memory. 将内容写入MemoryStream是不可能的,因为有太多数据要完全保留在内存中。 Is there a way I can somehow chain these two methods, or do I have to manually write an adaptor of some kind to go in between myself?
有没有办法可以以某种方式链接这两种方法,还是我必须手动编写某种适配器才能进入我自己之间?
Edit: 编辑:
one of the methods looks like this, which serializes the object to a stream: 其中一个方法如下所示,它将对象序列化为流:
object1.WriteToStream(Stream s)
while the other looks like this: 而另一个看起来像这样:
object2.Process(Stream input, Stream output)
The second method reads from the input stream, processes the data and writes it to another stream. 第二种方法从输入流中读取,处理数据并将其写入另一个流。 My problem is that I need to use the 2nd method to process the data generated by the
WriteToStream
method of the first object. 我的问题是我需要使用第二种方法来处理第一个对象的
WriteToStream
方法生成的数据。
Yes you have a possibility to "chain" the two methods. 是的,你有可能“链接”这两种方法。 But there are some prerequisite:
但有一些先决条件:
And here are sample code to do it. 以下是执行此操作的示例代码。 But this is not working code, it is just a skeleton
但这不是工作代码,它只是一个骨架
using System;
using System.Threading;
public class Example
{
// A semaphore that simulates a limited resource pool.
//
private static Semaphore _pool;
// A padding interval to make the output more orderly.
private static int _padding;
public static void Main()
{
// Create a semaphore that can satisfy up to three
// concurrent requests. Use an initial count of zero,
// so that the entire semaphore count is initially
// owned by the main program thread.
_pool = new Semaphore(0, 2);
Thread threadWrite = new Thread(new ParameterizedThreadStart(WriterThread));
Thread threadRead = new Thread(new ParameterizedThreadStart(ReadThread));
threadWrite.Start(commonStream);
threadRead.Start(commonStream);
// Wait for half a second, to allow all the
// threads to start and to block on the semaphore.
Thread.Sleep(500);
// The main thread starts out holding the entire
// semaphore count. Calling Release(3) brings the
// semaphore count back to its maximum value, and
// allows the waiting threads to enter the semaphore,
// up to three at a time.
//
Console.WriteLine("Main thread calls Release(3).");
_pool.Release(3);
Console.WriteLine("Main thread exits.");
}
private static void WriterThread(object objStream)
{
Stream stream = (Stream)objStream;
while (true)
{
// lock the semaphore, because you want to write the stream
_pool.WaitOne();
// your code goes here, to write the stream to some data, but not all
//release the pool, to indicate to the other thread, there are data in stream
_pool.Release();
if (IsAllDataWritten)
break;
}
}
private static void ReadThread(object objStream)
{
Stream stream = (Stream)objStream;
while (true)
{
// lock the semaphore, because you want to write the stream
_pool.WaitOne();
// your code goes here, to read and process the stream data
//release the pool, to indicate to the other thread, there are data in stream
_pool.Release();
if (AllDataIsReaded )
break;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.