using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication5
{
class Program
{
//true 代表有信号量
static ManualResetEvent e = new ManualResetEvent(true);
static AutoResetEvent auto_e = new AutoResetEvent(true);
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
test();
}
private static void test1()
{
//申请信号量,申请到信号量后,AutoResetEvent自动关闭信号量0
auto_e.WaitOne();
Console.WriteLine("test1");
//把信号量设置为1,其他线程可以使用
auto_e.Set();
}
private static void test()
{
//申请信号量
e.WaitOne();
//手动关闭信号量为0,如果不关闭的话,信号量仍然为1,其他线程函数也可以申请到信号量
e.Reset();
Console.WriteLine("test");
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication5
{
class Program
{
//true 代表有信号量
static ManualResetEvent e = new ManualResetEvent(true);
static AutoResetEvent auto_e = new AutoResetEvent(true);
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
test();
}
private static void test1()
{
//申请信号量,申请到信号量后,AutoResetEvent自动关闭信号量0
auto_e.WaitOne();
Console.WriteLine("test1");
//把信号量设置为1,其他线程可以使用
auto_e.Set();
}
private static void test()
{
//申请信号量
e.WaitOne();
//手动关闭信号量为0,如果不关闭的话,信号量仍然为1,其他线程函数也可以申请到信号量
e.Reset();
Console.WriteLine("test");
}
}
}