Global Mutex : Mutex « Thread « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Thread » Mutex 
23.7.1.Global Mutex
Imports System.Threading

Module Module1
    Public Class MutexSample
        Shared globalMutex1 As Mutex
        Shared globalMutex2 As Mutex

        Const Counter As Integer = 100

        Shared Event1 As AutoResetEvent = New AutoResetEvent(False)
        Shared Event2 As AutoResetEvent = New AutoResetEvent(False)
        Shared Event3 As AutoResetEvent = New AutoResetEvent(False)
        Shared Event4 As AutoResetEvent = New AutoResetEvent(False)

        Public Sub do1()
            Console.WriteLine("do1 started,  Mutex.WaitAll(Mutex())")
            Dim gMs(1As Mutex

            gMs(0= globalMutex1    
            gMs(1= globalMutex2

            WaitHandle.WaitAll(gMs)
            Thread.Sleep(2000)
            Console.WriteLine("do1 finished, Mutex.WaitAll(Mutex())")
            Event1.Set()
        End Sub

        Public Sub do2()
            Console.WriteLine("do2 started,  globalMutex1.WaitOne( )")
            globalMutex1.WaitOne()   
            Console.WriteLine("do2 finished, globalMutex1.WaitOne( )")
            Event2.Set()  
        End Sub

        Public Sub do3()

            Console.WriteLine("do3 started,  Mutex.WaitAny(Mutex())")
            Dim gMs(1As Mutex
            gMs(0= globalMutex1    
            gMs(1= globalMutex2
            Mutex.WaitAny(gMs)
            Console.WriteLine("do3 finished, Mutex.WaitAny(Mutex())")
            Event3.Set()     
        End Sub

        Public Sub do4() 
            Console.WriteLine("do4 started,  globalMutex2.WaitOne( )")
            globalMutex2.WaitOne()    
            Console.WriteLine("do4 finished, globalMutex2.WaitOne( )")
            Event4.Set()     
        End Sub

        Public Sub Run()     
            Console.WriteLine("MutexSample ...")
            globalMutex1 = New Mutex(True, "MyMutex"
            globalMutex2 = New Mutex(True)   
            Console.WriteLine(" - Main Owns globalMutex1 and globalMutex2")

            Dim evs(3As AutoResetEvent
            evs(0= Event1   
            evs(1= Event2   
            evs(2= Event3   
            evs(3= Event4   

            Dim tm As MutexSample = New MutexSample()
            Dim t1 As Thread = New Thread(New ThreadStart(AddressOf tm.do1))
            Dim t2 As Thread = New Thread(New ThreadStart(AddressOf tm.do2))
            Dim t3 As Thread = New Thread(New ThreadStart(AddressOf tm.do3))
            Dim t4 As Thread = New Thread(New ThreadStart(AddressOf tm.do4))

            t1.Start()     
            t2.Start()     
            t3.Start()     
            t4.Start()     

            Thread.Sleep(2000)
            Console.WriteLine(" - Main releases globalMutex1")
            globalMutex1.ReleaseMutex() 

            Thread.Sleep(1000)
            Console.WriteLine(" - Main releases globalMutex2")
            globalMutex2.ReleaseMutex() 

        End Sub
    End Class 
    Sub main()
        Dim ms As MutexSample = New MutexSample()
        ms.Run()
    End Sub
End Module
MutexSample ...
 - Main Owns globalMutex1 and globalMutex2
do1 started,  Mutex.WaitAll(Mutex())
do2 started,  globalMutex1.WaitOne( )
do3 started,  Mutex.WaitAny(Mutex())
do4 started,  globalMutex2.WaitOne( )
 - Main releases globalMutex1
do3 finished, Mutex.WaitAny(Mutex())
^CTerminate batch job (Y/N)? n
23.7.Mutex
23.7.1.Global Mutex
23.7.2.Mutex synchronizes access to a protected resource
23.7.3.Not attach the security object to a Mutex object.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.