no***********@gmail.com wrote:
I have two threads in my application, one that decodes audio to a
stream and one that encodes a stream to a file.
If I run them as two seperate processes I can pipe the output from one
to the input of the other - and all is well. The writes to standard
output are obviously synchronous, so the decoding occurs at the rate at
which the encoder can process...
How do I replicate this with two threads in one process? I guess what
I want is a single frame FIFO queue or something similar. Does there
exist something in the framework to achieve this, or do I have to roll
my own?
Maybe this can give you some ideas:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace E
{
public class MainClass
{
private const int PORT = 12345;
private static TextWriter savout;
private static TextReader savin;
private static void StartPipeOut()
{
TcpListener list = new TcpListener(IPAddress.Any, PORT);
list.Start();
TcpClient cli = list.AcceptTcpClient();
TextWriter sw = new StreamWriter(cli.GetStream());
savout = Console.Out;
Console.SetOut(sw);
}
private static void EndPipeOut()
{
Console.Out.Close();
Console.SetOut(savout);
}
private static void StartPipeIn()
{
Thread.Sleep(0);
TcpClient cli = new TcpClient("localhost", PORT);
TextReader sr = new StreamReader(cli.GetStream());
savin = Console.In;
Console.SetIn(sr);
}
private static void EndPipeIn()
{
Console.In.Close();
Console.SetIn(savin);
}
public static void First()
{
StartPipeOut();
int n = 0;
for(int i = 0; i < 100000; i++)
{
Console.WriteLine("This is a test");
n++;
}
EndPipeOut();
Console.WriteLine("Wrote " + n);
}
public static void Second()
{
StartPipeIn();
int n = 0;
string line;
while((line = Console.ReadLine()) != null)
{
n++;
}
EndPipeIn();
Console.WriteLine("Read " + n);
}
public static void Main(string[] args)
{
Thread first = new Thread(new ThreadStart(First));
first.Start();
Thread second = new Thread(new ThreadStart(Second));
second.Start();
first.Join();
second.Join();
Console.ReadLine();
}
}
}
Arne
PS: Sorry for all the static but I did not want to spend time
inventig some classes.