VB.NET中的线程操作(Thread,Mutex)
2012-01-06 21:24
726 查看
VB.NET中的线程操作(Thread,Mutex)
By Tyouvivi 2008/04/16VB.NET 2005 中线程操作稍有点麻烦,Abort()方法调用时会产生System.Threading.ThreadAbortException异常信息,而继续已挂起的线程方法Resume()和挂起方法Suspend()也不再支持,线程的暂停、再开、结束不是很方便,在此使用Mutex类(同步基元)进行线程的操作。
Mutex介绍
当两个或更多线程需要同时访问一个共享资源时,系统需要使用同步机制来确保一次只有一个线程使用该资源。Mutex 是同步基元,它只向一个线程授予对共享资源的独占访问权。如果一个线程获取了互斥体,则要获取该互斥体的第二个线程将被挂起,直到第一个线程释放该互斥体。
可以使用 WaitHandle..::.WaitOne 方法请求互斥体的所属权。拥有互斥体的线程可以在对 WaitOne 的重复调用中请求相同的互斥体而不会阻止其执行。但线程必须调用 ReleaseMutex 方法同样多的次数以释放互斥体的所属权。Mutex 类强制线程标识,因此互斥体只能由获得它的线程释放。相反,Semaphore 类不强制线程标识。
如果线程在拥有互斥体时终止,则称此互斥体被放弃。将此 mutex 的状态设置为收到信号,下一个等待线程将获得所有权。从 .NET Framework 2.0 版开始,在获取被放弃 mutex 的下一个线程中将引发 AbandonedMutexException。在 .NET Framework 2.0 版之前,这样不会引发任何异常。
ThreadState Form
三个按钮: ButtonStart、ButtonPause、ButtonStop
两个状态Label: 执行状况、LabelStatus
代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
Public Class ThreadState
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 状态
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Enum ThreadState As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Runing = 1 ''执行中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Paused = 2 ''暂停
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stoped = 3 ''结束
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Enum
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 线程
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private _thread As System.Threading.Thread
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 同步基元,用于进程间同步。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private _mutex As Threading.Mutex
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 线程状态
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private _state As ThreadState
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 线程调用外部控件方法(Delegate)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <param name="text"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Delegate Sub SetTextCalled(ByVal [text] As String)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' Form Load
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <param name="sender"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <param name="e"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub ThreadTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''状态初期化
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me._state = ThreadState.Stoped
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''按钮初期化
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetButtonState()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetText("未开始...")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 执行
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub Execute()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim i As Integer = 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''执行直到结束
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Do While Not Me._state = ThreadState.Stoped
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_mutex.WaitOne()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_mutex.ReleaseMutex()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetText("执行中... " & " " & i)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i += 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Threading.Thread.Sleep(1000)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Loop
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetText("结束")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 线程开始,再开
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <param name="sender"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <param name="e"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If Me._state = ThreadState.Stoped Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_thread = New System.Threading.Thread(AddressOf Execute) ''线程开始
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''同步基元初始化
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_mutex = New Threading.Mutex
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_thread.Start()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ElseIf Me._state = ThreadState.Paused Then ''线程再开
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''同步基元释放(线程继续)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_mutex.ReleaseMutex()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''线程状态
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me._state = ThreadState.Runing
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetButtonState()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 暂停
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <param name="sender"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <param name="e"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub ButtonPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPause.Click
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'SetText("Pasue")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''同步基元暂停(线程暂停)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_mutex.WaitOne()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonStart.Enabled = True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me._state = ThreadState.Paused
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetButtonState()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 结束
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <param name="sender"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <param name="e"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If Me._state = ThreadState.Paused Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''同步基元释放(线程继续)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_mutex.ReleaseMutex()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''状态设置
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me._state = ThreadState.Stoped
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SetButtonState()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_thread = Nothing
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 执行状态
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <param name="str"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub SetText(ByVal str As String)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''线程中调用时需要Invoke(InvokeRequired = True)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If Me.LabelStatus.InvokeRequired Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim d As New SetTextCalled(AddressOf SetText)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.Invoke(d, New Object() {str})
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.LabelStatus.Text = str
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' 按钮状态
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
''' <remarks></remarks>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Private Sub SetButtonState()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Select Case Me._state
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Case ThreadState.Runing
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonStart.Enabled = False
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonPause.Enabled = True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonStop.Enabled = True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Case ThreadState.Paused
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonStart.Text = "Resume"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonStart.Enabled = True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonPause.Enabled = False
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonStop.Enabled = True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Case ThreadState.Stoped
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonStart.Text = "Start"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonStart.Enabled = True
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonPause.Enabled = False
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Me.ButtonStop.Enabled = False
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Select
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Sub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
End Class
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
运行效果如下:
相关文章推荐
- VB.NET中的线程操作(Thread,Mutex)
- VB.NET 线程间操作无效错误的解决办法
- [VB.NET]在线程里频繁的进行委托操作主界面的ui是不是效果不好?
- Vb.net Thread线程实例
- c++的boost库多线程(Thread)编程(线程操作,互斥体mutex,条件变量)详解
- boost库多线程(Thread)编程(线程操作,互斥体mutex,条件变量)
- c++的boost库多线程(Thread)编程(线程操作,互斥体mutex,条件变量)详解
- net 提供了Thread类用于线程的操作
- 用VB.net实现对.ini文件的读写操作的类
- [vb.net]解决 "在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级"
- VB.NET中字符串操作函数
- VB.NET学习(二)数据库操作
- Java Thread 线程 操作线程
- 关于VB.NET出现“集合已修改;可能无法执行枚举操作”错误的解决
- C#(VB.NET)操作Windows自带的防火墙 之 综述篇
- C#(VB.NET)操作Windows自带的防火墙 之 启用(开启)/禁用(关闭)防火墙
- VB.net应用技巧4: 跨线程调用控件
- 在VB.NET里操作文本文件
- vb.net操作EXCEL
- DataGridView在vb.net中的操作技巧