Click here to Skip to main content
15,891,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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
Posted

1 solution

private void btn_cancel_Click(object sender, RoutedEventArgs e)
{

thread.Abort();
}
may this is helpful for you
 
Share this answer
 
Comments
Jocelyne El Khoury 6-Sep-13 5:36am    
Error : 'Abort' is not a member of 'System.Threading.AutoResetEvent'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900