| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 服务进程编程 >> 实例分析正文
最新推荐 更多内容
Threading-WaitSynchronization的实例
Threading-WaitSynchronization的实例
作者:sdk    来源:microsoft     更新时间:2008-1-9

所属分类 : Threading

WaitSynchronization的实例

代码如下:

'-----------------------------------------------------------------------
'  This file is part of the Microsoft .NET Framework SDK Code Samples.
'
'  Copyright (C) Microsoft Corporation.  All rights reserved.
'
'This source code is intended only as a supplement to Microsoft
'Development Tools and/or on-line documentation.  See these other
'materials for detailed information regarding Microsoft code samples.
'
'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'PARTICULAR PURPOSE.
'-----------------------------------------------------------------------
'=====================================================================
'  File:      Mutex.vb
'
'  Summary:   Demonstrates how to wait for an object to go signalled
'
'---------------------------------------------------------------------


Option Explicit On
Option Strict On


Imports System
Imports System.Threading


Class Resource
    Private m As New Mutex()
       
    Public Sub Access(threadNum As Int32)
        m.WaitOne()
        Try
            Console.WriteLine("Start Resource access (Thread={0})", threadNum)
            Thread.Sleep(500)
            Console.WriteLine("Stop  Resource access (Thread={0})", threadNum)
        Finally
            m.ReleaseMutex()
        End Try
    End Sub 'Access
End Class 'Resource

 

Class App
    Private Shared numAsyncOps As Int32 = 5
    Private Shared asyncOpsAreDone As New AutoResetEvent(False)
    Private Shared res As New Resource()
   
   
    Public Shared Sub Main()
        Dim threadNum As Int32
        For threadNum = 0 To 4
            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf UpdateResource), threadNum)
        Next threadNum
       
        asyncOpsAreDone.WaitOne()
        Console.WriteLine("All operations have completed.")
    End Sub 'Main
   
   
   
    ' The callback method's signature MUST match that of a System.Threading.TimerCallback
    ' delegate (it takes an Object parameter and returns void)
    Shared Sub UpdateResource(state As Object)
        res.Access(CType(state, Int32))
        If Interlocked.Decrement(numAsyncOps) = 0 Then
            asyncOpsAreDone.Set()
        End If
    End Sub 'UpdateResource
End Class 'App

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    Threading-Timers的实例
    Threading-ReaderWriterSynchronization的实例
    Threading-Pools的实例
    Threading-MonitorSynchronization的实例
    Threading-JoinThread的实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]