码迷,mamicode.com
首页 > 编程语言 > 详细

线程池管理线程及线程间通信

时间:2014-09-18 18:10:34      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   art   sp   cti   on   c   line   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleTest
{
public class ClassThread
{
//监听线程事件
private ManualResetEvent myResetEvent = new ManualResetEvent(false);
private ManualResetEvent myResetEvent2 = new ManualResetEvent(false);
public bool set()
{
myResetEvent.Set();
return true;
}

public bool set2()
{
myResetEvent2.Set();
return true;
}

private void ThreadOne(object stateInfo)
{

for (int i = 0; i < 100; i++)
{
Console.WriteLine("123");
}
set();
myResetEvent2.WaitOne();
Console.WriteLine("123!!!!!!!!!");
}
private void ThreadTwo(object stateInfo)
{

myResetEvent.WaitOne();
Console.WriteLine("456");

Func<bool> func = new Func<bool>(MethodTwo);
func.Invoke();

Console.WriteLine("901");
Console.WriteLine("901");
Console.WriteLine("901");

}
private bool MethodTwo()
{
Console.WriteLine("0000000000000000000000000000000000000000000000");

return set2();
}
public void run()
{
Thread _ThreadAsyncRunner = new Thread(new ThreadStart(ThreadProc_AsyncRunner));
_ThreadAsyncRunner.Start();
}
private void ThreadProc_AsyncRunner()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.ThreadOne));
ThreadPool.QueueUserWorkItem(new WaitCallback(this.ThreadTwo));
}
}
}

 

test:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
Console.SetBufferSize(100,1200);
ClassThread threads = new ClassThread();
threads.run();
Console.WriteLine("11111");
Console.ReadKey();
}
}
}

线程池管理线程及线程间通信

标签:io   ar   for   art   sp   cti   on   c   line   

原文地址:http://www.cnblogs.com/lxc-binary/p/3979459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!