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

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

转载于:https://www.cnblogs.com/lxc-binary/p/3979459.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值