使用流的C#链接方法

时间:2016-05-03 09:57:42

标签: c#

我在c#中有两个不同的对象,它们使用流来公开方法。

其中一个公开了一个方法,该方法将Stream作为参数并写入流,而另一个公开了一个方法,该方法将Stream作为参数并从中读取。

将内容写入MemoryStream是不可能的,因为有太多数据要完全保留在内存中。有没有办法可以以某种方式链接这两种方法,还是我必须手动编写某种类型的适配器才能进入我自己之间?

编辑:

其中一个方法如下所示,它将对象序列化为流:

object1.WriteToStream(Stream s)

而另一个看起来像这样:

object2.Process(Stream input, Stream output)

第二种方法从输入流读取,处理数据并将其写入另一个流。我的问题是我需要使用第二种方法来处理第一个对象的WriteToStream方法生成的数据。

1 个答案:

答案 0 :(得分:0)

是的,您可以“链接”这两种方法。 但是有一些先决条件:

  • 输出流(写流的参数)必须是全局的,或者从两个函数都可以访问。
  • 这两个函数都可以在不同的线程上运行,以同步运行。使用Task.Run()或其他Thread
  • 要将其同步到线程,可以使用Semaphore

这是执行此操作的示例代码。但这不是有效的代码,只是一个框架

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;
        }
    }
}