using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleCsharp
{
class Proxy
{
System.Threading.Thread worker;
Queue<WaitCallback> queue;
// List<WaitCallback> list;
System.Threading.Semaphore sm = new Semaphore(1, 1);
public Proxy()
{
queue = new Queue<WaitCallback>();
worker = new System.Threading.Thread(this.run);
worker.Name = "worker";
worker.Start();
}
public void run()
{
for (; ; )
{
WaitCallback wc = null;
sm.WaitOne();
if (queue.Count >= 1)
{
wc = queue.Dequeue();
}
sm.Release();
if (wc != null)
{
wc.Invoke(null);
}
}
}
public void handle(WaitCallback wc)
{
sm.WaitOne();
queue.Enqueue(wc);
sm.Release();
}
}
class Caller
{
Proxy pro;
public Caller()
{
pro = new Proxy();
}
public void doFunc(int i)
{
pro.handle(delegate {
System.Console.Out.WriteLine(Thread.CurrentThread.Name+":" + i);
});
}
}
class Program
{
static void Main(string[] args)
{
Caller c = new Caller();
for (int i = 0; i < 100; ++i)
{
c.doFunc(i);
}
while(true)
Thread.Sleep(100);
}
}
}