new Mutex(false), WaitOne : Mutex « Thread « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Thread » MutexScreenshots 
new Mutex(false), WaitOne
 
using System;
using System.Threading;
   
class Database
{
    static Mutex mutex = new Mutex(false);
   
    public static void SaveData(string text)
    {
        mutex.WaitOne();
        Console.WriteLine("Database.SaveData - Started");
        Console.WriteLine("Database.SaveData - Working");
        for (int i = 0; i < 100; i++)
        {
            Console.Write(text);
        }
        Console.WriteLine("\nDatabase.SaveData - Ended");
        mutex.Close();
    }
}
   
class ThreadMutexApp{
    public static void WorkerThreadMethod1() {
        Console.WriteLine("Worker thread #1 - Started");
        Database.SaveData("x");
        Console.WriteLine("Worker thread #1 - Returned from Output");
    }
   
    public static void WorkerThreadMethod2() {
      Console.WriteLine("Worker thread #2 - Started");
      Database.SaveData("o");
      Console.WriteLine("Worker thread #2 - Returned from Output");
    }
   
    public static void Main() {
        ThreadStart worker1 = new ThreadStart(WorkerThreadMethod1);
        ThreadStart worker2 = new ThreadStart(WorkerThreadMethod2);
   
        Console.WriteLine("Main - Creating worker threads");
   
        Thread t1 = new Thread(worker1);
        Thread t2 = new Thread(worker2);
   
        t1.Start();
        t2.Start();
   
        Console.ReadLine();
    }
}

 
Related examples in the same category
1.new Mutex
2.new Mutex(true, "MutexExample", out ownsMutex)), ReleaseMutex
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.