using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class Class1
{
static bool flag = true;
static void DoWork()
{
Console.WriteLine(" worker thread started, now waiting on event...");
while (flag)
{
}
Console.WriteLine(" worker thread reactivated, now exiting...");
}
static void Main()
{
Console.WriteLine("main thread starting worker thread...");
Thread t = new Thread(DoWork);
t.Start();
Console.WriteLine("main thrad sleeping for 1 second...");
Thread.Sleep(1000);
Console.WriteLine("main thread signaling worker thread...");
flag = false;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class Class2
{
static AutoResetEvent mEvent=new AutoResetEvent(false);
//static ManualResetEvent mEvent = new ManualResetEvent(false);
static void DoWork()
{
Console.WriteLine(" worker thread started, now waiting on event...");
mEvent.WaitOne();
Console.WriteLine(" worker thread reactivated, now exiting...");
}
static void Main()
{
Console.WriteLine("main thread starting worker thread...");
Thread t = new Thread(DoWork);
t.Start();
Console.WriteLine("main thrad sleeping for 1 second...");
Thread.Sleep(1000);
Console.WriteLine("main thread signaling worker thread...");
mEvent.Set();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class Class3
{
static AutoResetEvent mEvent = new AutoResetEvent(false);
//static ManualResetEvent mEvent = new ManualResetEvent(false);
static void DoWork()
{
Console.WriteLine(" worker thread started, now waiting on event...");
for (int i = 0; i < 3; i++)
{
mEvent.WaitOne();
//mEvent.Reset();
Console.WriteLine(" worker thread reactivated, now exiting...");
}
}
static void Main()
{
Console.WriteLine("main thread starting worker thread...");
Thread t = new Thread(DoWork);
t.Start();
for (int i = 0; i < 3; i++)
{
Thread.Sleep(1000);
Console.WriteLine("main thread signaling worker thread...");
mEvent.Set();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class Class4
{
public static AutoResetEvent mEvent = new AutoResetEvent(false);
public static void trmain()
{
Thread tr = Thread.CurrentThread;
Console.WriteLine("thread: waiting for an event");
mEvent.WaitOne();
Console.WriteLine("thread: got an event");
for (int x = 0; x < 10; x++)
{
Thread.Sleep(1000);
Console.WriteLine(tr.Name + ": " + x);
}
}
static void Main(string[] args)
{
Thread thrd1 = new Thread(new ThreadStart(trmain));
thrd1.Name = "thread1";
thrd1.Start();
for (int x = 0; x < 10; x++)
{
Thread.Sleep(900);
Console.WriteLine("Main:" + x);
if (5 == x) mEvent.Set();
}
while (thrd1.IsAlive)
{
Thread.Sleep(1000);
Console.WriteLine("Main: waiting for thread to stop");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class other
{
static void Main(string[] args)
{
Random randomGenerator = new Random();
AutoResetEvent[] resets=new AutoResetEvent[5];
for (int i = 0; i < 5; i++)
{
resets[i] = new AutoResetEvent(false);
int wTime = randomGenerator.Next(10)+1;
worker w = new worker(wTime, resets[i]);
Thread thrd1 = new Thread(new ThreadStart(w.work));
thrd1.Start();
}
WaitHandle.WaitAll(resets);
Console.WriteLine("ALL worker done - main exiting.");
}
}
public class worker
{
public string name;
public int wTime;
public AutoResetEvent mEvent;
public worker(int w, AutoResetEvent m)
{
name = w.ToString();
wTime = w * 1000;
mEvent = m;
}
public void work()
{
Console.WriteLine(name + " worker thread waiting for " + wTime + "....");
Thread.Sleep(wTime);
Console.WriteLine(name + " worker thread back...");
mEvent.Set();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AutoResetEventTest
{
class Program
{
const int numIterations = 10;
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
static AutoResetEvent ChangeEvent = new AutoResetEvent(false);
//static ManualResetEvent myResetEvent = new ManualResetEvent(false);
//static ManualResetEvent ChangeEvent = new ManualResetEvent(false);
static int number; //这是关键资源
static void Main()
{
Thread payMoneyThread = new Thread(new ThreadStart(PayMoneyProc));
payMoneyThread.Name = "付钱线程";
Thread getBookThread = new Thread(new ThreadStart(GetBookProc));
getBookThread.Name = "取书线程";
payMoneyThread.Start();
getBookThread.Start();
for (int i = 1; i <= numIterations; i++)
{
Console.WriteLine("买书线程:数量{0}", i);
number = i;
//Signal that a value has been written.
myResetEvent.Set();
//ChangeEvent.Set();
Thread.Sleep(10);
}
payMoneyThread.Abort();
getBookThread.Abort();
}
static void PayMoneyProc()
{
while (true)
{
myResetEvent.WaitOne();
//myResetEvent.Reset();
Console.WriteLine("{0}:数量{1}", Thread.CurrentThread.Name, number);
ChangeEvent.Set();
}
}
static void GetBookProc()
{
while (true)
{
ChangeEvent.WaitOne();
//ChangeEvent.Reset();
Console.WriteLine("{0}:数量{1}", Thread.CurrentThread.Name, number);
Console.WriteLine("------------------------------------------");
//Thread.Sleep(0);
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有