Csharp/C Sharp by API/System.Threading/Mutex — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:08, 26 мая 2010
Mutex.ReleaseMutex()
using System;
using System.Threading;
class MainClass {
public static void Main() {
bool ownsMutex;
using (Mutex mutex = new Mutex(true, "MutexExample", out ownsMutex)) {
if (ownsMutex) {
Console.ReadLine();
mutex.ReleaseMutex();
} else {
Console.WriteLine("Another instance of this application " +
" already owns the mutex named MutexExample.");
}
}
}
}
Mutex.WaitOne()
using System;
using System.Threading;
public class MainClass
{
private static int Runs = 0;
static Mutex mtx;
public static void CountUp()
{
while (Runs < 10)
{
mtx.WaitOne();
int Temp = Runs;
Temp++;
Console.WriteLine(Thread.CurrentThread.Name + " " + Temp);
Thread.Sleep(1000);
Runs = Temp;
mtx.ReleaseMutex();
}
}
public static void Main()
{
mtx = new Mutex(false, "RunsMutex");
Thread t2 = new Thread(new ThreadStart(CountUp));
t2.Name = "t2";
Thread t3 = new Thread(new ThreadStart(CountUp));
t3.Name = "t3";
t2.Start();
t3.Start();
}
}