Reputation: 3865
i've a loop which gets data from some Websites and it takes about an hour for that loop to get the data and populate it in a database, this loop is in a thread and i want to add a button that pauses the thread until the button is clicked again and it resumes where it stopped. how can i do this in a safe way?
Upvotes: 1
Views: 1718
Reputation: 2722
Try this:
Public Sub PauseThreadButton_Click(sender As System.Object, e As System.EventArgs) Handles PauseThreadButton.Click
'REM: Next line puts your thread in a suspended state
TheThread.Suspend()
End Sub
To resume:
Public Sub ResumeThreadButton_Click(sender As System.Object, e As System.EventArgs) Handles ResumeThreadButton.Click
'REM: Next line resumes your thread
TheThread.Resume()
End Sub
Upvotes: 0
Reputation: 252
Here is a simple Windows Console application; it uses a Boolean flag to indicate whether the thread should process or not. I hope this points you in the right direction.
Module Module1
Private _ThreadControl_Run As Boolean = False
Sub Main()
Dim thread As New System.Threading.Thread(AddressOf ThreadWorker)
thread.IsBackground = True
thread.Start()
_ThreadControl_Run = True
Console.WriteLine("Main() _ThreadControl_Run = " & _ThreadControl_Run.ToString())
System.Threading.Thread.Sleep(1000)
_ThreadControl_Run = False
Console.WriteLine("Main() _ThreadControl_Run = " & _ThreadControl_Run.ToString())
System.Threading.Thread.Sleep(1000)
_ThreadControl_Run = True
Console.WriteLine("Main() _ThreadControl_Run = " & _ThreadControl_Run.ToString())
System.Threading.Thread.Sleep(1000)
_ThreadControl_Run = False
Console.WriteLine("Main() _ThreadControl_Run = " & _ThreadControl_Run.ToString())
System.Threading.Thread.Sleep(1000)
End Sub
Private Sub ThreadWorker()
Do While True
If (_ThreadControl_Run) Then
Console.WriteLine(" ThreadWorker()")
System.Threading.Thread.Sleep(100)
End If
Loop
End Sub
End Module
Upvotes: 1