vb.net使用线程池的简单介绍( 二 )


多线程应用程序NET Framework的一个很重要的特性是 可以在不使用第三方工具或不支持的Visual Basic技巧情况下 使用Visual Basic创建多线程应用程序 NET Framework的多线程支持是由System Threading名称空间中的类和接口提供的 因此所有的 NET语言都能够以相同的方式创建和处理线程 System Threading Thread是一个核心类 提供了对创建和控制线程的支持 要创建一个线程 你可以创建一个新的System Threading Thread对象 将构造函数传递给ThreadStart代理 这个代理提供了这个线程开始执行的方法 当你准备启动这个新的线程时 可以调用Thread Start() (请参阅清单 ) 当你开始创建多线程应用程序时 你很快就会认识到需要控制对共享资源的访问 如共享的类变量 NET Framework还包括几个类和数据类型 你可以使用它们对两个线程执行的动作进行同步 在最简单的情况中 你由一个需要从不同的线程中进行更新的共享变量 要这样做 你可以使用System Threading Interlocked类 例如 你可以通过编写Interlocked Increment(num)或Interlocked Decrement(num)分别使名为num的共享变量递增或递减 你还可以使用Interlocked将变量设为某一特定值 或检查两个变量是否相等 除了这种简单情况以外 你可以使用 NET Framework类来执行更复杂的线程同步 如事件和互斥体的同步 所有都来自于 NET Framework内部 而无须使用Win API Imports System IO注释 The namespace System Threading注释 contains the Thread classImports System ThreadingModule Module Private count As LongSub Main()注释 Create the ThreadStart delegateDim tStart As ThreadStart = New _ ThreadStart(AddressOf StartCounting)注释 Create the threadDim t As Thread = New Thread(tStart)Console WriteLine( Enter q to quit )t Start() 注释 start the threadWhile (Console Read()asc( q ))注释 repeat the loop until the user enters qEnd Whilet Stop() 注释 tell thread to stop processingt Join() 注释 wait until the thread finishesEnd SubSub StartCounting()Do注释 use Interlocked Increment in case 注释 another thread is accessing the same variableInterlocked Increment(count)Console WriteLine( _ After incrementing count is : count)Thread Sleep( )LoopEnd SubEnd Module 清单 使用Visual Basic NET创建线程 你创建了一个新线程 将它传递给一个ThreadStart代理 然后调用Thread Start()启动这个线程 你可以通过调用Thread Stop()来中止这个线程 然后调用Thread Join()等待它完成关闭操作 一个线程可以使用System Threading Interlocked来使变量递增或递减 此外 NET Framework提供了一个方便的机制来对工作排队 并将起分配给线程池中的某个线程 在处理多个并发工作项目或工作请求的服务器应用程序中 这非常有用 例如 对于等待输入文件 然后将它们导入到数据库中去的应用程序 可能会对每个输入文件进行排队 以在线程池中的某个单独的线程上进行处理 System Threading ThreadPool类允许你使用共享的QueueUserWorkItem方法对工作进行排队 以前要这样做 你必须得创建和管理自己的线程池 你又需要在基础设施工作而不是在解决商务问题上花大量的时间和精力文件系统监控我曾经遇到过一些应用程序 需要等待和处理某个特定目录中的文件 例如 将数据从文件导入到数据库中去的应用程序 数据文件可以从某个大型机上下载 或者被转移到某个输入目录中 该应用程序将它们导入到数据库中 你不用经常地轮询该目录检查是否有新文件 可以等待生成新文件的通知 你可以在Visual Basic 中使用Win API来做到这一点 而在Visual Basic NET中你可以使用 NET Framework类来做这项工作 但是在 NET中实施文件监控与在 NET中完成其他工作的方法更加一致 因此学习曲线是最小的 你可以使用System IO FileSystemWatcher NET类对文件系统进行监视 它提供了一些属性 允许你设置监控的路径 指定是对文件还是子目录层次的变化感兴趣 System IO FileSystemWatcher还允许你指定需要监控的文件名和文件类型(例如 * xml是指监控所有XML文件的变化) 最后 你可以指定感兴趣的变化类型 例如 只对新建文件 文件属性的变化或文件大小的变化(请参阅清单 )感兴趣 在你设置了监控内容后 你需要钩住用于感兴趣的各种事件的事件处理程序 FileSystemWatcher事件有Changed Created Deleted Error和Renamed 要处理某个事件 首先你需要编写一个与FileSystemEventHandler代理相同声明的事件处理程序 然后将这个处理程序添加到FileSystemWatcher类中 这个基于代理的体系结构允许你为同一个事件添加多个处理程序 或者对于多个事件使用同一个处理程序 而你不能使用Visual Basic 做到这一点 注释 System IO contains the 注释 file monitoring classes and typesImports System IOModule Module Sub Main() 注释 FileSystemWatcher does the real work Dim fw As New FileSystemWatcher() 注释 WaitForChangedResult is what you注释 get back when a change occurs Dim result As WaitForChangedResult 注释 set the path to monitor fw Path = C:WINNT注释 tell it whether to watch files or directories fw Target = WatcherTarget File 注释 tell it whether to include subdirs fw IncludeSubdirectories = False 注释 hook up handlers AddHandler fw Created New FileSystemEventHandler(AddressOf OnFileNotify) 注释 enable the watcher fw Enabled = True DoConsole WriteLine( Beginning to monitor ) 注释 this is where we actually wait注释 waiting blocks execution for the specified timeoutresult = fw WaitForChanged(WatcherChangeTypes All )Console WriteLine( Hit Enter to continue q to quit ) Loop While (Console ReadLineq )End Sub注释 This is the delegate that gets 注释 called when a file is created Public Sub OnFileNotify(ByVal source As Object ByVal e As FileSystemEventArgs)Console WriteLine( Notification received for file change type is _e FullPath e ChangeType) End SubEnd Module 清单 使用FileSystemWatcher监控某个文件夹是否有新文件

推荐阅读