本文整理汇总了VB.NET中System.Threading.Semaphore.Semaphore构造函数的典型用法代码示例。如果您正苦于以下问题:VB.NET Semaphore构造函数的具体用法?VB.NET Semaphore怎么用?VB.NET Semaphore使用的例子?那么恭喜您, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Threading.Semaphore
的用法示例。
在下文中一共展示了Semaphore构造函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Threading
Public Class Example
' A semaphore that simulates a limited resource pool.
'
Private Shared _pool As Semaphore
' A padding interval to make the output more orderly.
Private Shared _padding As Integer
<MTAThread> _
Public Shared Sub Main()
' Create a semaphore that can satisfy up to three
' concurrent requests. Use an initial count of zero,
' so that the entire semaphore count is initially
' owned by the main program thread.
'
_pool = New Semaphore(0, 3)
' Create and start five numbered threads.
'
For i As Integer = 1 To 5
Dim t As New Thread(New ParameterizedThreadStart(AddressOf Worker))
'Dim t As New Thread(AddressOf Worker)
' Start the thread, passing the number.
'
t.Start(i)
Next i
' Wait for half a second, to allow all the
' threads to start and to block on the semaphore.
'
Thread.Sleep(500)
' The main thread starts out holding the entire
' semaphore count. Calling Release(3) brings the
' semaphore count back to its maximum value, and
' allows the waiting threads to enter the semaphore,
' up to three at a time.
'
Console.WriteLine("Main thread calls Release(3).")
_pool.Release(3)
Console.WriteLine("Main thread exits.")
End Sub
Private Shared Sub Worker(ByVal num As Object)
' Each worker thread begins by requesting the
' semaphore.
Console.WriteLine("Thread {0} begins " _
& "and waits for the semaphore.", num)
_pool.WaitOne()
' A padding interval to make the output more orderly.
Dim padding As Integer = Interlocked.Add(_padding, 100)
Console.WriteLine("Thread {0} enters the semaphore.", num)
' The thread's "work" consists of sleeping for
' about a second. Each thread "works" a little
' longer, just to make the output more orderly.
'
Thread.Sleep(1000 + padding)
Console.WriteLine("Thread {0} releases the semaphore.", num)
Console.WriteLine("Thread {0} previous semaphore count: {1}", _
num, _
_pool.Release())
End Sub
End Class
示例2: Example
' 导入命名空间
Imports System.Threading
Public Class Example
<MTAThread> _
Public Shared Sub Main()
' Create a Semaphore object that represents the named
' system semaphore "SemaphoreExample3". The semaphore has a
' maximum count of five. The initial count is also five.
' There is no point in using a smaller initial count,
' because the initial count is not used if this program
' doesn't create the named system semaphore, and with
' this method overload there is no way to tell. Thus, this
' program assumes that it is competing with other
' programs for the semaphore.
'
Dim sem As New Semaphore(5, 5, "SemaphoreExample3")
' Attempt to enter the semaphore three times. If another
' copy of this program is already running, only the first
' two requests can be satisfied. The third blocks. Note
' that in a real application, timeouts should be used
' on the WaitOne calls, to avoid deadlocks.
'
sem.WaitOne()
Console.WriteLine("Entered the semaphore once.")
sem.WaitOne()
Console.WriteLine("Entered the semaphore twice.")
sem.WaitOne()
Console.WriteLine("Entered the semaphore three times.")
' The thread executing this program has entered the
' semaphore three times. If a second copy of the program
' is run, it will block until this program releases the
' semaphore at least once.
'
Console.WriteLine("Enter the number of times to call Release.")
Dim n As Integer
If Integer.TryParse(Console.ReadLine(), n) Then
sem.Release(n)
End If
Dim remaining As Integer = 3 - n
If (remaining) > 0 Then
Console.WriteLine("Press Enter to release the remaining " _
& "count ({0}) and exit the program.", remaining)
Console.ReadLine()
sem.Release(remaining)
End If
End Sub
End Class
示例3: Example
' 导入命名空间
Imports System.Threading
Public Class Example
<MTAThread> _
Public Shared Sub Main()
' The value of this variable is set by the semaphore
' constructor. It is True if the named system semaphore was
' created, and False if the named semaphore already existed.
'
Dim semaphoreWasCreated As Boolean
' Create a Semaphore object that represents the named
' system semaphore "SemaphoreExample". The semaphore has a
' maximum count of five, and an initial count of two. The
' Boolean value that indicates creation of the underlying
' system object is placed in semaphoreWasCreated.
'
Dim sem As New Semaphore(2, 5, "SemaphoreExample", _
semaphoreWasCreated)
If semaphoreWasCreated Then
' If the named system semaphore was created, its count is
' set to the initial count requested in the constructor.
' In effect, the current thread has entered the semaphore
' three times.
'
Console.WriteLine("Entered the semaphore three times.")
Else
' If the named system semaphore was not created,
' attempt to enter it three times. If another copy of
' this program is already running, only the first two
' requests can be satisfied. The third blocks.
'
sem.WaitOne()
Console.WriteLine("Entered the semaphore once.")
sem.WaitOne()
Console.WriteLine("Entered the semaphore twice.")
sem.WaitOne()
Console.WriteLine("Entered the semaphore three times.")
End If
' The thread executing this program has entered the
' semaphore three times. If a second copy of the program
' is run, it will block until this program releases the
' semaphore at least once.
'
Console.WriteLine("Enter the number of times to call Release.")
Dim n As Integer
If Integer.TryParse(Console.ReadLine(), n) Then
sem.Release(n)
End If
Dim remaining As Integer = 3 - n
If (remaining) > 0 Then
Console.WriteLine("Press Enter to release the remaining " _
& "count ({0}) and exit the program.", remaining)
Console.ReadLine()
sem.Release(remaining)
End If
End Sub
End Class
示例4: Example
' 导入命名空间
Imports System.Threading
Imports System.Security.AccessControl
Friend Class Example
<MTAThread> _
Friend Shared Sub Main()
Const semaphoreName As String = "SemaphoreExample5"
Dim sem As Semaphore = Nothing
Dim doesNotExist as Boolean = False
Dim unauthorized As Boolean = False
' Attempt to open the named semaphore.
Try
' Open the semaphore with (SemaphoreRights.Synchronize
' Or SemaphoreRights.Modify), to enter and release the
' named semaphore.
'
sem = Semaphore.OpenExisting(semaphoreName)
Catch ex As WaitHandleCannotBeOpenedException
Console.WriteLine("Semaphore 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 semaphore does not exist.
' (2) The semaphore exists, but the current user doesn't
' have access. (3) The semaphore exists and the user has
' access.
'
If doesNotExist Then
' The semaphore does not exist, so create it.
'
' The value of this variable is set by the semaphore
' constructor. It is True if the named system semaphore was
' created, and False if the named semaphore already existed.
'
Dim semaphoreWasCreated As Boolean
' Create an access control list (ACL) that denies the
' current user the right to enter or release the
' semaphore, but allows the right to read and change
' security information for the semaphore.
'
Dim user As String = Environment.UserDomainName _
& "\" & Environment.UserName
Dim semSec As New SemaphoreSecurity()
Dim rule As New SemaphoreAccessRule(user, _
SemaphoreRights.Synchronize Or SemaphoreRights.Modify, _
AccessControlType.Deny)
semSec.AddAccessRule(rule)
rule = New SemaphoreAccessRule(user, _
SemaphoreRights.ReadPermissions Or _
SemaphoreRights.ChangePermissions, _
AccessControlType.Allow)
semSec.AddAccessRule(rule)
' Create a Semaphore object that represents the system
' semaphore named by the constant 'semaphoreName', with
' maximum count three, initial count three, and the
' specified security access. The Boolean value that
' indicates creation of the underlying system object is
' placed in semaphoreWasCreated.
'
sem = New Semaphore(3, 3, semaphoreName, _
semaphoreWasCreated, semSec)
' If the named system semaphore was created, it can be
' used by the current instance of this program, even
' though the current user is denied access. The current
' program enters the semaphore. Otherwise, exit the
' program.
'
If semaphoreWasCreated Then
Console.WriteLine("Created the semaphore.")
Else
Console.WriteLine("Unable to create the semaphore.")
Return
End If
ElseIf unauthorized Then
' Open the semaphore to read and change the access
' control security. The access control security defined
' above allows the current user to do this.
'
Try
sem = Semaphore.OpenExisting(semaphoreName, _
SemaphoreRights.ReadPermissions Or _
SemaphoreRights.ChangePermissions)
' Get the current ACL. This requires
' SemaphoreRights.ReadPermissions.
Dim semSec As SemaphoreSecurity = sem.GetAccessControl()
Dim user As String = Environment.UserDomainName _
& "\" & Environment.UserName
' First, the rule that denied the current user
' the right to enter and release the semaphore must
' be removed.
Dim rule As New SemaphoreAccessRule(user, _
SemaphoreRights.Synchronize Or SemaphoreRights.Modify, _
AccessControlType.Deny)
semSec.RemoveAccessRule(rule)
' Now grant the user the correct rights.
'
rule = New SemaphoreAccessRule(user, _
SemaphoreRights.Synchronize Or SemaphoreRights.Modify, _
AccessControlType.Allow)
semSec.AddAccessRule(rule)
' Update the ACL. This requires
' SemaphoreRights.ChangePermissions.
sem.SetAccessControl(semSec)
Console.WriteLine("Updated semaphore security.")
' Open the semaphore with (SemaphoreRights.Synchronize
' Or SemaphoreRights.Modify), the rights required to
' enter and release the semaphore.
'
sem = Semaphore.OpenExisting(semaphoreName)
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unable to change permissions: {0}", _
ex.Message)
Return
End Try
End If
' Enter the semaphore, and hold it until the program
' exits.
'
Try
sem.WaitOne()
Console.WriteLine("Entered the semaphore.")
Console.WriteLine("Press the Enter key to exit.")
Console.ReadLine()
sem.Release()
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unauthorized access: {0}", _
ex.Message)
End Try
End Sub
End Class