vb.net system.threading.thread要求线程清理并停止
我是vb.net多线程的新手,我面临一个问题。 对于线程,我使用system.threading.thread函数 我知道我可以使用thread.abort调用停止线程。但这不是一个好的选择,我已经搜索了一点,并开始知道这一点。这也不起作用,因为我正在运行的线程需要在取消之前进行一些清理 所以,我尝试做一些动作,将变量设置为UI线程式vb.net system.threading.thread要求线程清理并停止,vb.net,multithreading,Vb.net,Multithreading,我是vb.net多线程的新手,我面临一个问题。 对于线程,我使用system.threading.thread函数 我知道我可以使用thread.abort调用停止线程。但这不是一个好的选择,我已经搜索了一点,并开始知道这一点。这也不起作用,因为我正在运行的线程需要在取消之前进行一些清理 所以,我尝试做一些动作,将变量设置为UI线程式 dim bolStop as Boolean 所以,如果我的UI线程将该布尔值设置为true,那么所有正在运行的线程都可以访问/注意到该更改,并可以在停止/取消
dim bolStop as Boolean
所以,如果我的UI线程将该布尔值设置为true,那么所有正在运行的线程都可以访问/注意到该更改,并可以在停止/取消线程本身之前启动清理过程
希望这有意义
致意
编辑:根据@monkey的建议,将1个源粘贴到此处
这是主窗体Form1.vb的代码,我想我在这里需要一些东西,在线程模块类上也需要一些东西,因此,下面我也将粘贴模块代码
Public Class Form1
Private objThreadList As ArrayList
Dim bolStop As Boolean
Private Sub cmd1_Click(sender As System.Object, e As System.EventArgs) Handles cmd1.Click
Dim intTLoop As Integer
sPrepareThreading()
For intTLoop = 1 To 5
sSendThread("", "Zakir", "Biplob")
Next
'sFinishThreading()
MessageBox.Show("Done!")
End Sub
Sub sPrepareThreading()
Dim intTLoop As Integer
objThreadList = New ArrayList
For intTLoop = 1 To 3
objThreadList.Add(Nothing)
Next intTLoop
End Sub
Sub sSendThread(strDraft As String, ByVal strTitle As String, ByVal strSubHeader As String)
Dim bolFalied As Boolean
Dim intTLoop As Integer
Dim objItem As System.Windows.Forms.ListViewItem
Dim objTData As objThreadCrossData
Dim objThreadClass As clsThread
Dim objNewThread As System.Threading.Thread
Zakir_RecheckThread:
My.Application.DoEvents()
If bolStop = True Then
Exit Sub
End If
bolFalied = True
For intTLoop = 1 To 3
bolFalied = True
If objThreadList(intTLoop - 1) Is Nothing Then
bolFalied = False
Else
If CType(objThreadList(intTLoop - 1), System.Threading.Thread).IsAlive = False Then
bolFalied = False
Else
bolFalied = True
End If
End If
If bolFalied = False Then
objTData = New objThreadCrossData
objTData.ID = ListView1.Items.Count
objTData.bolStop = False
objTData.strTime = DateTime.Now.ToString
objItem = ListView1.Items.Add(strTitle)
objItem.SubItems.Add(strSubHeader)
objItem = Nothing
objThreadClass = New clsThread(objTData, "", Me)
objNewThread = New System.Threading.Thread(AddressOf objThreadClass.StartThread)
objNewThread.IsBackground = True
objNewThread.Start()
objThreadList(intTLoop - 1) = objNewThread
Exit For
End If
Next
If bolFalied = True Then
GoTo Zakir_RecheckThread
Exit Sub
End If
End Sub
Public Sub ReceiveThreadMessage(ByVal objCrosData As Object)
Dim objTDataV1 As objThreadCrossData
objTDataV1 = CType(objCrosData, objThreadCrossData)
ListView1.Items(objTDataV1.ID).SubItems(1).Text = objTDataV1.strProgress
End Sub
Private Sub cmd2_Click(sender As System.Object, e As System.EventArgs) Handles cmd2.Click
REM This is where i like to stop the running thred either by setting bolStop = True and all running thead check that variable.
REM or by chate the thread (Thread Data Object Property naemd "bolStop" value"
REM please help me out...
End Sub
End Class
线程模块clsThread.vb的代码:
Public Class clsThread
Private m_Counter As Integer = 0
Private m_MainWindow As Form
Dim m_ThreadCrorssData As objThreadCrossData
Private Delegate Sub NotifyMainWindow(ByVal objCrosData As objThreadCrossData)
'We need an object of this deletegate
Private m_NotifyMainWindow As NotifyMainWindow
Public Sub New(ByVal objCrossData As Object, ByVal strXML As String, ByRef MainWindow As Form1)
m_MainWindow = MainWindow
m_ThreadCrorssData = CType(objCrossData, objThreadCrossData)
'We need to point our delegate to the Method, which we want to call from this thread
m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage
End Sub
Public Sub StartThread()
While m_Counter < 100
m_Counter = m_Counter + 1
rem for example here i like to check for the value that whether user asked to stop
m_ThreadCrorssData.strProgress = CStr(m_Counter)
m_MainWindow.Invoke(m_NotifyMainWindow, m_ThreadCrorssData)
rem for example also here i like to check for the value that whether user asked to stop
'wait for some time before continuing loop
System.Threading.Thread.Sleep(1000)
End While
End Sub
End Class
你的线程在做什么?他们正在轮询线程吗?这些或异常长时间运行的线程几乎是您需要中止的唯一类型。我会让短时间运行的线程优雅地完成,让GC处理事情,而不是中止它们。对于轮询线程,我将调用thread.Abort,捕获ThreadAbortedException并从那里进行清理。在任何情况下,一些显示您的程序流的示例代码将帮助您更好地回答问题。我正在寻找类似于锁布尔值之类的东西。我不知道那叫什么。然而,正如@monkey所建议的,这里是下载我的源代码项目的链接。它位于包含vb.net 2010源项目文件的zip文件中。这里是链接:请只发布相关的代码位。。。没有人真的希望下载一个压缩的二进制文件并打开一个项目,然后尝试找到相关的区域。@monkey,先生,现在请检查我已经在主帖子中添加了源代码。。谢谢你的建议。。。
Module mdlCMM
Public Class objThreadCrossData
Property strProgress As String
Property bolStop As Boolean = False
Property strTime As String
Property ID As Integer
Property intStatusNum As Integer
Property strStatusValue As String
End Class
End Module