no***********@g mail.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.Sock ets;
using System.Threadin g;
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(IPA ddress.Any, PORT);
list.Start();
TcpClient cli = list.AcceptTcpC lient();
TextWriter sw = new StreamWriter(cl i.GetStream());
savout = Console.Out;
Console.SetOut( sw);
}
private static void EndPipeOut()
{
Console.Out.Clo se();
Console.SetOut( savout);
}
private static void StartPipeIn()
{
Thread.Sleep(0) ;
TcpClient cli = new TcpClient("loca lhost", PORT);
TextReader sr = new StreamReader(cl i.GetStream());
savin = Console.In;
Console.SetIn(s r);
}
private static void EndPipeIn()
{
Console.In.Clos e();
Console.SetIn(s avin);
}
public static void First()
{
StartPipeOut();
int n = 0;
for(int i = 0; i < 100000; i++)
{
Console.WriteLi ne("This is a test");
n++;
}
EndPipeOut();
Console.WriteLi ne("Wrote " + n);
}
public static void Second()
{
StartPipeIn();
int n = 0;
string line;
while((line = Console.ReadLin e()) != null)
{
n++;
}
EndPipeIn();
Console.WriteLi ne("Read " + n);
}
public static void Main(string[] args)
{
Thread first = new Thread(new ThreadStart(Fir st));
first.Start();
Thread second = new Thread(new ThreadStart(Sec ond));
second.Start();
first.Join();
second.Join();
Console.ReadLin e();
}
}
}
Arne
PS: Sorry for all the static but I did not want to spend time
inventig some classes.