在vb.net中按下按钮时停止线程 [英] Stop a thread when a button is pressed in vb.net
本文介绍了在vb.net中按下按钮时停止线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用同步线程,但我正在调用每次完成for循环时启动线程的函数但是我需要能够停止线程我单击按钮停止我该怎么做?
请注意我在vs 2010上使用vb.net
这是我的代码:
i'm using Synchronizing Threads but i'm calling the function that starts the thread everytime the for loop is completed but i need to be able to stop the thread i click a the button stop how can i do that?
note that i'm using vb.net on vs 2010
here is my code:
Private Shared thread As New System.Threading.AutoResetEvent(False)
Private Sub start_btn_Click(sender As System.Object, e As System.EventArgs) Handles start_btn.Click
StartTask()
End Sub
Sub StartTask()
Dim Tpool As System.Threading.ThreadPool
Dim arg As String = "SomeArg"
ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback( _
AddressOf Task), arg) ' Queue a task.
thread.WaitOne() ' Wait for the thread to call Set.
writeMsg("Thread is done. at ", "list_rpt")
StartTask()
End Sub
Sub Task(ByVal Arg As Object)
writeMsg("Thread is starting at ", "list_rpt")
System.Threading.Thread.Sleep(4000) ' Wait 4 seconds.
DoWork()
End Sub
Public Sub DoWork()
MTProcessTable = selectMtProcess()
mtCount = MTProcessTable.Rows.Count
writeMsg(mtCount & " rows selected at ", "list_rpt")
For Each row As DataRow In MTProcessTable.Rows
process = New Process_class
process.id = row("mt_id")
process.mo = row("mt_mo")
process.mt = row("mt_mt")
process.datain = row("mt_datain")
keyid = validateKey(process.datain)
MOTable = selectMO(process.mo, process.mt)
moRowNb = MOTable.Rows.Count()
MO = New MO_class
If moRowNb <> 0 Then
MOrow = MOTable.Rows(0)
MO.newuser = MOrow("newuser")
MO.sim_id = MOrow("sim_id")
End If
Try
Select Case keyid
Case 1
If moRowNb = 0 Then
If insertMO(process.mo, process.mt) Then
writeMsg("1 MO inserted/updated at ", "rpt_txt")
message = msgTable.Rows(0)(1).ToString()
MsgBox(message)
Else
writeMsg("1 MO could not be inserted at ", "rpt_txt")
End If
End if
Case 2
If moRowNb = 0 Then
writeMsg("1 process with MO not active and datain 'yes' at ", "rpt_txt")
message = msgTable.Rows(7)(1).ToString()
MsgBox(message)
End if
Catch ex As Exception
logFile("executeTimer ----" & ex.Message)
updateProcessed(process.id, ex.Message, 10)
Finally
updateProcessed(process.id, message, 1)
End Try
Next row
thread.Set()
End Sub
Private Sub stop_btn_Click(sender As System.Object, e As System.EventArgs) Handles stop_btn.Click
I need to stop my thread here !
End Sub
推荐答案
private void btn_cancel_Click(object sender,RoutedEventArgs e)
{
thread.Abort();
}
这对你有帮助
private void btn_cancel_Click(object sender, RoutedEventArgs e)
{
thread.Abort();
}
may this is helpful for you
这篇关于在vb.net中按下按钮时停止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文