本文整理汇总了VB.NET中System.Threading.Mutex.Mutex构造函数的典型用法代码示例。如果您正苦于以下问题:VB.NET Mutex构造函数的具体用法?VB.NET Mutex怎么用?VB.NET Mutex使用的例子?那么, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Threading.Mutex
的用法示例。
在下文中一共展示了Mutex构造函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Test
' This example shows how a Mutex is used to synchronize access
' to a protected resource. Unlike Monitor, Mutex can be used with
' WaitHandle.WaitAll and WaitAny, and can be passed across
' AppDomain boundaries.
Imports System.Threading
Class Test
' Create a new Mutex. The creating thread does not own the
' Mutex.
Private Shared mut As New Mutex()
Private Const numIterations As Integer = 1
Private Const numThreads As Integer = 3
<MTAThread> _
Shared Sub Main()
' Create the threads that will use the protected resource.
Dim i As Integer
For i = 1 To numThreads
Dim myThread As New Thread(AddressOf MyThreadProc)
myThread.Name = [String].Format("Thread{0}", i)
myThread.Start()
Next i
' The main thread exits, but the application continues to
' run until all foreground threads have exited.
End Sub
Private Shared Sub MyThreadProc()
Dim i As Integer
For i = 1 To numIterations
UseResource()
Next i
End Sub
' This method represents a resource that must be synchronized
' so that only one thread at a time can enter.
Private Shared Sub UseResource()
' Wait until it is safe to enter.
mut.WaitOne()
Console.WriteLine("{0} has entered protected area", _
Thread.CurrentThread.Name)
' Place code to access non-reentrant resources here.
' Simulate some work
Thread.Sleep(500)
Console.WriteLine("{0} is leaving protected area" & vbCrLf, _
Thread.CurrentThread.Name)
' Release Mutex.
mut.ReleaseMutex()
End Sub
End Class
示例2: Test
' 导入命名空间
Imports System.Threading
Class Test
' Create a new Mutex. The creating thread owns the
' Mutex.
Private Shared mut As New Mutex(True)
Private Const numIterations As Integer = 1
Private Const numThreads As Integer = 3
<MTAThread> _
Shared Sub Main()
' Create the threads that will use the protected resource.
Dim i As Integer
For i = 1 To numThreads
Dim myThread As New Thread(AddressOf MyThreadProc)
myThread.Name = [String].Format("Thread{0}", i)
myThread.Start()
Next i
' Wait one second before allowing other threads to
' acquire the Mutex.
Console.WriteLine("Creating thread owns the Mutex.")
Thread.Sleep(1000)
Console.WriteLine("Creating thread releases the Mutex." & vbCrLf)
mut.ReleaseMutex()
End Sub
Private Shared Sub MyThreadProc()
Dim i As Integer
For i = 1 To numIterations
UseResource()
Next i
End Sub
' This method represents a resource that must be synchronized
' so that only one thread at a time can enter.
Private Shared Sub UseResource()
' Wait until it is safe to enter.
mut.WaitOne()
Console.WriteLine("{0} has entered protected area", _
Thread.CurrentThread.Name)
' Place code to access non-reentrant resources here.
' Simulate some work
Thread.Sleep(500)
Console.WriteLine("{0} is leaving protected area" & vbCrLf, _
Thread.CurrentThread.Name)
' Release Mutex.
mut.ReleaseMutex()
End Sub
End Class
输出:
Creating thread owns the Mutex. Creating thread releases the Mutex. Thread1 has entered the protected area Thread1 is leaving the protected area Thread2 has entered the protected area Thread2 is leaving the protected area Thread3 has entered the protected area Thread3 is leaving the protected area
示例3: Test
' 导入命名空间
Imports System.Threading
Public Class Test
Public Shared Sub Main()
' Create the named mutex. Only one system object named
' "MyMutex" can exist; the local Mutex object represents
' this system object, regardless of which process or thread
' caused "MyMutex" to be created.
Dim m As New Mutex(False, "MyMutex")
' Try to gain control of the named mutex. If the mutex is
' controlled by another thread, wait for it to be released.
Console.WriteLine("Waiting for the Mutex.")
m.WaitOne()
' Keep control of the mutex until the user presses
' ENTER.
Console.WriteLine("This application owns the mutex. " _
& "Press ENTER to release the mutex and exit.")
Console.ReadLine()
m.ReleaseMutex()
End Sub
End Class
示例4: Test
' This example shows how a named mutex is used to signal between
' processes or threads.
' Run this program from two (or more) command windows. Each process
' creates a Mutex object that represents the named mutex "MyMutex".
' The named mutex is a system object whose lifetime is bounded by the
' lifetimes of the Mutex objects that represent it. The named mutex
' is created when the first process creates its local Mutex; in this
' example, the named mutex is owned by the first process. The named
' mutex is destroyed when all the Mutex objects that represent it
' have been released.
' The second process (and any subsequent process) waits for earlier
' processes to release the named mutex.
Imports System.Threading
Public Class Test
<MTAThread> _
Public Shared Sub Main()
' Set this variable to false if you do not want to request
' initial ownership of the named mutex.
Dim requestInitialOwnership As Boolean = True
Dim mutexWasCreated As Boolean
' Request initial ownership of the named mutex by passing
' true for the first parameter. Only one system object named
' "MyMutex" can exist; the local Mutex object represents
' this system object. If "MyMutex" is created by this call,
' then mutexWasCreated contains true; otherwise, it contains
' false.
Dim m As New Mutex(requestInitialOwnership, "MyMutex", _
mutexWasCreated)
' This thread owns the mutex only if it both requested
' initial ownership and created the named mutex. Otherwise,
' it can request the named mutex by calling WaitOne.
If Not (requestInitialOwnership And mutexWasCreated) Then
Console.WriteLine("Waiting for the named mutex.")
m.WaitOne()
End If
' Once the process has gained control of the named mutex,
' hold onto it until the user presses ENTER.
Console.WriteLine("This process owns the named mutex. " _
& "Press ENTER to release the mutex and exit.")
Console.ReadLine()
' Call ReleaseMutex to allow other threads to gain control
' of the named mutex. If you keep a reference to the local
' Mutex, you can call WaitOne to request control of the
' named mutex.
m.ReleaseMutex()
End Sub
End Class
示例5: Example
' 导入命名空间
Imports System.Threading
Imports System.Security.AccessControl
Friend Class Example
<MTAThread> _
Friend Shared Sub Main()
Const mutexName As String = "MutexExample4"
Dim m As Mutex = Nothing
Dim doesNotExist as Boolean = False
Dim unauthorized As Boolean = False
' The value of this variable is set by the mutex
' constructor. It is True if the named system mutex was
' created, and False if the named mutex already existed.
'
Dim mutexWasCreated As Boolean
' Attempt to open the named mutex.
Try
' Open the mutex with (MutexRights.Synchronize Or
' MutexRights.Modify), to enter and release the
' named mutex.
'
m = Mutex.OpenExisting(mutexName)
Catch ex As WaitHandleCannotBeOpenedException
Console.WriteLine("Mutex does not exist.")
doesNotExist = True
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unauthorized access: {0}", ex.Message)
unauthorized = True
End Try
' There are three cases: (1) The mutex does not exist.
' (2) The mutex exists, but the current user doesn't
' have access. (3) The mutex exists and the user has
' access.
'
If doesNotExist Then
' The mutex does not exist, so create it.
' Create an access control list (ACL) that denies the
' current user the right to enter or release the
' mutex, but allows the right to read and change
' security information for the mutex.
'
Dim user As String = Environment.UserDomainName _
& "\" & Environment.UserName
Dim mSec As New MutexSecurity()
Dim rule As New MutexAccessRule(user, _
MutexRights.Synchronize Or MutexRights.Modify, _
AccessControlType.Deny)
mSec.AddAccessRule(rule)
rule = New MutexAccessRule(user, _
MutexRights.ReadPermissions Or _
MutexRights.ChangePermissions, _
AccessControlType.Allow)
mSec.AddAccessRule(rule)
' Create a Mutex object that represents the system
' mutex named by the constant 'mutexName', with
' initial ownership for this thread, and with the
' specified security access. The Boolean value that
' indicates creation of the underlying system object
' is placed in mutexWasCreated.
'
m = New Mutex(True, mutexName, mutexWasCreated, mSec)
' If the named system mutex was created, it can be
' used by the current instance of this program, even
' though the current user is denied access. The current
' program owns the mutex. Otherwise, exit the program.
'
If mutexWasCreated Then
Console.WriteLine("Created the mutex.")
Else
Console.WriteLine("Unable to create the mutex.")
Return
End If
ElseIf unauthorized Then
' Open the mutex to read and change the access control
' security. The access control security defined above
' allows the current user to do this.
'
Try
m = Mutex.OpenExisting(mutexName, _
MutexRights.ReadPermissions Or _
MutexRights.ChangePermissions)
' Get the current ACL. This requires
' MutexRights.ReadPermissions.
Dim mSec As MutexSecurity = m.GetAccessControl()
Dim user As String = Environment.UserDomainName _
& "\" & Environment.UserName
' First, the rule that denied the current user
' the right to enter and release the mutex must
' be removed.
Dim rule As New MutexAccessRule(user, _
MutexRights.Synchronize Or MutexRights.Modify, _
AccessControlType.Deny)
mSec.RemoveAccessRule(rule)
' Now grant the user the correct rights.
'
rule = New MutexAccessRule(user, _
MutexRights.Synchronize Or MutexRights.Modify, _
AccessControlType.Allow)
mSec.AddAccessRule(rule)
' Update the ACL. This requires
' MutexRights.ChangePermissions.
m.SetAccessControl(mSec)
Console.WriteLine("Updated mutex security.")
' Open the mutex with (MutexRights.Synchronize
' Or MutexRights.Modify), the rights required to
' enter and release the mutex.
'
m = Mutex.OpenExisting(mutexName)
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unable to change permissions: {0}", _
ex.Message)
Return
End Try
End If
' If this program created the mutex, it already owns
' the mutex.
'
If Not mutexWasCreated Then
' Enter the mutex, and hold it until the program
' exits.
'
Try
Console.WriteLine("Wait for the mutex.")
m.WaitOne()
Console.WriteLine("Entered the mutex.")
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unauthorized access: {0}", _
ex.Message)
End Try
End If
Console.WriteLine("Press the Enter key to exit.")
Console.ReadLine()
m.ReleaseMutex()
m.Dispose()
End Sub
End Class