Mutex.WaitOne() : Mutex « System.Threading « VB.Net by API

Home
VB.Net by API
1.Microsoft.VisualBasic
2.Microsoft.Win32
3.System
4.System.Collections
5.System.Collections.Generic
6.System.Collections.Specialized
7.System.ComponentModel
8.System.Configuration.ConfigurationSettings
9.System.Data
10.System.Data.Odbc
11.System.Data.OleDb
12.System.Data.OracleClient
13.System.Data.SqlClient
14.System.Diagnostics
15.System.Drawing
16.System.Drawing.Drawing2D
17.System.Drawing.Imaging
18.System.Drawing.Printing
19.System.Drawing.Text
20.System.Globalization
21.System.IO
22.System.IO.IsolatedStorage
23.System.IO.Ports
24.System.Media
25.System.Messaging
26.System.Net
27.System.Net.Sockets
28.System.Reflection
29.System.Runtime.InteropServices
30.System.Runtime.Remoting.Channels
31.System.Runtime.Serialization
32.System.Runtime.Serialization.Formatters.Binary
33.System.Runtime.Serialization.Formatters.Soap
34.System.Security.Cryptography
35.System.Security.Cryptography.X509Certificates
36.System.Security.Permissions
37.System.Security.Principal
38.System.ServiceProcess
39.System.Text
40.System.Text.RegularExpressions
41.System.Threading
42.System.Web
43.System.Web.Mail
44.System.Windows.Forms
45.System.Xml
46.System.Xml.Schema
47.System.Xml.Serialization
48.System.Xml.Xsl
VB.Net
VB.Net Tutorial
VB.Net by API » System.Threading » Mutex 
Mutex.WaitOne()
  

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

   
    
  
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.