本文主要是介绍VB.NET中的线程操作(Thread,Mutex),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
VB.NET中的线程操作(Thread,Mutex)
By Tyouvivi 2008/04/16
VB.NET 2005 中线程操作稍有点麻烦,Abort()方法调用时会产生System.Threading.ThreadAbortException异常信息,而继续已挂起的线程方法Resume()和挂起方法Suspend()也不再支持,线程的暂停、再开、结束不是很方便,在此使用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
代码如下:
''' <summary>
''' 状态
''' </summary>
''' <remarks></remarks>
Enum ThreadStateEnum ThreadState As Integer
Runing = 1 ''执行中
Paused = 2 ''暂停
Stoped = 3 ''结束
End Enum
''' <summary>
''' 线程
''' </summary>
''' <remarks></remarks>
Private _thread As System.Threading.Thread
''' <summary>
''' 同步基元,用于进程间同步。
''' </summary>
''' <remarks></remarks>
Private _mutex As Threading.Mutex
''' <summary>
''' 线程状态
''' </summary>
''' <remarks></remarks>
Private _state As ThreadState
''' <summary>
''' 线程调用外部控件方法(Delegate)
''' </summary>
''' <param name="text"></param>
''' <remarks></remarks>
Delegate Sub SetTextCalled()Sub SetTextCalled(ByVal [text] As String)
''' <summary>
''' Form Load
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub ThreadTest_Load()Sub ThreadTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''状态初期化
Me._state = ThreadState.Stoped
''按钮初期化
SetButtonState()
SetText("未开始...")
End Sub
''' <summary>
''' 执行
''' </summary>
''' <remarks></remarks>
Private Sub Execute()Sub Execute()
Dim i As Integer = 1
''执行直到结束
Do While Not Me._state = ThreadState.Stoped
_mutex.WaitOne()
_mutex.ReleaseMutex()
SetText("执行中... " & " " & i)
i += 1
Threading.Thread.Sleep(1000)
Loop
SetText("结束")
End Sub
''' <summary>
''' 线程开始,再开
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub ButtonStart_Click()Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
If Me._state = ThreadState.Stoped Then
_thread = New System.Threading.Thread(AddressOf Execute) ''线程开始
''同步基元初始化
_mutex = New Threading.Mutex
_thread.Start()
ElseIf Me._state = ThreadState.Paused Then ''线程再开
''同步基元释放(线程继续)
_mutex.ReleaseMutex()
End If
''线程状态
Me._state = ThreadState.Runing
SetButtonState()
End Sub
''' <summary>
''' 暂停
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub ButtonPause_Click()Sub ButtonPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPause.Click
'SetText("Pasue")
''同步基元暂停(线程暂停)
_mutex.WaitOne()
Me.ButtonStart.Enabled = True
Me._state = ThreadState.Paused
SetButtonState()
End Sub
''' <summary>
''' 结束
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub ButtonStop_Click()Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click
If Me._state = ThreadState.Paused Then
''同步基元释放(线程继续)
_mutex.ReleaseMutex()
End If
''状态设置
Me._state = ThreadState.Stoped
SetButtonState()
_thread = Nothing
End Sub
''' <summary>
''' 执行状态
''' </summary>
''' <param name="str"></param>
''' <remarks></remarks>
Private Sub SetText()Sub SetText(ByVal str As String)
''线程中调用时需要Invoke(InvokeRequired = True)
If Me.LabelStatus.InvokeRequired Then
Dim d As New SetTextCalled(AddressOf SetText)
Me.Invoke(d, New Object() {str})
Else
Me.LabelStatus.Text = str
End If
End Sub
''' <summary>
''' 按钮状态
''' </summary>
''' <remarks></remarks>
Private Sub SetButtonState()Sub SetButtonState()
Select Case Me._state
Case ThreadState.Runing
Me.ButtonStart.Enabled = False
Me.ButtonPause.Enabled = True
Me.ButtonStop.Enabled = True
Case ThreadState.Paused
Me.ButtonStart.Text = "Resume"
Me.ButtonStart.Enabled = True
Me.ButtonPause.Enabled = False
Me.ButtonStop.Enabled = True
Case ThreadState.Stoped
Me.ButtonStart.Text = "Start"
Me.ButtonStart.Enabled = True
Me.ButtonPause.Enabled = False
Me.ButtonStop.Enabled = False
End Select
End Sub
End Class
运行效果如下:
这篇关于VB.NET中的线程操作(Thread,Mutex)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!