线程操作问题 现有两个线程a和b,操作的是同一个文件,怎么样才能保证,a线程操作完毕后才启动b线程进行操作. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那何必用两个线程?调用Join,可以阻塞当前线程直到那个线程结束 用AutoResetEvent 去做详细资料可以查msdn using System;using System.Threading;class Resource { Mutex m = new Mutex(); public void Access(Int32 threadNum) { m.WaitOne(); try { Console.WriteLine("Start Resource access (Thread={0})", threadNum); Thread.Sleep(500); Console.WriteLine("Stop Resource access (Thread={0})", threadNum); } finally { m.ReleaseMutex(); } }}class App { static Int32 numAsyncOps = 5; static AutoResetEvent asyncOpsAreDone = new AutoResetEvent(false); static Resource res = new Resource(); public static void Main() { for (Int32 threadNum = 0; threadNum < 5; threadNum++) { ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateResource), threadNum); } asyncOpsAreDone.WaitOne(); Console.WriteLine("All operations have completed."); } // The callback method's signature MUST match that of a System.Threading.TimerCallback // delegate (it takes an Object parameter and returns void) static void UpdateResource(Object state) { res.Access((Int32) state); if (Interlocked.Decrement(ref numAsyncOps) == 0) asyncOpsAreDone.Set(); }}VS自己带的例子, winForm按钮链接样式实现 急求两直线的交点坐标公式 c# 的按键问题 C#中如何实现横向和纵向排版打印 紧,求救!!!!多谢,读取TXT文件问题!!!! 请问如何提取本机时间(年-月-日-小时-分)? 新手问一个初级问题:怎么样用C# 打开一个已经存在的.xls文件? 对于.net,大家看一下我这样理解对不对。 c#.net与c++.net哪个好?? C#中是否有类似VC中CRectTracker的类或控件. 看不懂英文版的计算机类的书。。和中文对着看好还是买中文版的好呢? 再问?arraylise
调用Join,可以阻塞当前线程直到那个线程结束
详细资料可以查msdn
using System.Threading;
class Resource {
Mutex m = new Mutex(); public void Access(Int32 threadNum) {
m.WaitOne();
try {
Console.WriteLine("Start Resource access (Thread={0})", threadNum);
Thread.Sleep(500);
Console.WriteLine("Stop Resource access (Thread={0})", threadNum);
}
finally {
m.ReleaseMutex();
}
}
}
class App {
static Int32 numAsyncOps = 5;
static AutoResetEvent asyncOpsAreDone = new AutoResetEvent(false);
static Resource res = new Resource(); public static void Main() {
for (Int32 threadNum = 0; threadNum < 5; threadNum++) {
ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateResource), threadNum);
} asyncOpsAreDone.WaitOne();
Console.WriteLine("All operations have completed.");
}
// The callback method's signature MUST match that of a System.Threading.TimerCallback
// delegate (it takes an Object parameter and returns void)
static void UpdateResource(Object state) {
res.Access((Int32) state);
if (Interlocked.Decrement(ref numAsyncOps) == 0)
asyncOpsAreDone.Set();
}
}
VS自己带的例子,