Hi,
My plan is to have my main thread start a second thread and also show a form that will inform the user of the state of the second thread and also allow the user to select the cancel button that will stop the second thread. so far I've got the application to start the second thread - display the form, the form text updates with the information about the amount of retries the second thread has completed. My problem is that I can't press the cancel button on the form to stop the second thread. I'm really new to threading so any help about why I can't press the cancel button would be very helpful.
the code is as follows, i've modified a little for the sake of brevity.
method that creates thread
code for thread
code on the click even for the cancel button on the backgroundForm from the code above
Thank you in advance for your assistance.
Liam
My plan is to have my main thread start a second thread and also show a form that will inform the user of the state of the second thread and also allow the user to select the cancel button that will stop the second thread. so far I've got the application to start the second thread - display the form, the form text updates with the information about the amount of retries the second thread has completed. My problem is that I can't press the cancel button on the form to stop the second thread. I'm really new to threading so any help about why I can't press the cancel button would be very helpful.
the code is as follows, i've modified a little for the sake of brevity.
method that creates thread
Public Function ThreadController() As String Dim result As GetQrResultsEnum = GetQrResultsEnum.Success Dim returnString As String = Nothing LastProcessMessagae = "" 'create thread paramaters Dim ApiThreadParamaters As ThreadParamaters = New ThreadParamaters() 'Create an array of events to wait on Dim waitHandles As WaitHandle() = New WaitHandle() {ApiThreadParamaters.ThreadCancled, ApiThreadParamaters.ThreadCompleted} Dim retryCount As Integer = 0 Dim backgroundForm As RetryNotificationForm = Nothing Try Dim exitLoop As Boolean = False While exitLoop = False 'create thread Dim backgroundThread As ParameterizedThreadStart = New ParameterizedThreadStart(AddressOf GetQrCode) Dim thread As Thread = New Thread(backgroundThread) thread.Name = "communicate..." 'start the thread thread.Start(ApiThreadParamaters) If backgroundForm Is Nothing Then backgroundForm = New RetryNotificationForm() backgroundForm.Initialise(ApiThreadParamaters.ThreadCancled) backgroundForm.Show() End If backgroundForm.SetMessage("operation processing..." & retrycount) 'wait for the thread to time out or for one of the wait handles to occur Dim waitResult As Integer = WaitHandle.WaitAny(waitHandles, 30000) If waitResult = 0 Then exitLoop = True ElseIf waitResult = 1 Then 'operation was completed sucessfully If ApiThreadParamaters.success = GetQrResultsEnum.Success Then result = GetQrResultsEnum.Success LastProcessMessagae = ApiThreadParamaters.message returnString = ApiThreadParamaters.QrString exitLoop = True ElseIf ApiThreadParamaters.success = GetQrResultsEnum.Fail Then ''show the background info form result = GetQrResultsEnum.Fail If backgroundForm Is Nothing Then backgroundForm = New RetryNotificationForm() backgroundForm.Initialise(ApiThreadParamaters.ThreadCancled) backgroundForm.Show() End If backgroundForm.SetMessage("operation processing " & retrycount) retryCount = retryCount + 1 'reset the completed event ApiThreadParamaters.ThreadCompleted.Reset() If retryCount > m_MaxRetries Then result = GetQrResultsEnum.Fail exitLoop = True End If ElseIf waitResult = WaitHandle.WaitTimeout Then 'tell the thread to end ApiThreadParamaters.ThreadCancled.Set() ''show the background info form result = GetQrResultsEnum.FailTimeOut If backgroundForm Is Nothing Then backgroundForm = New RetryNotificationForm() backgroundForm.Initialise(ApiThreadParamaters.ThreadCancled) backgroundForm.Show() End If backgroundForm.SetMessage("operation processing..." & retry count) retryCount = retryCount + 1 returnString = Nothing 'reset the cancled event ApiThreadParamaters.ThreadCancled.Reset() If retryCount > m_MaxRetries Then result = GetQrResultsEnum.Fail exitLoop = True End If End If End While Catch ex As Exception Finally ''log errors if they exist. If backgroundForm IsNot Nothing Then backgroundForm.Hide() backgroundForm.Dispose() backgroundForm = Nothing End If End Try Return returnString End Function
code for thread
Private Sub GetQrCode(state As Object) Dim apiThreadParamaters As ThreadParamaters = state If apiThreadParamaters Is Nothing Then If state Is Nothing Then Throw New ArgumentNullException("State") Else Throw New ArgumentException("state was not of the expected type") End If End If Try If apiThreadParamaters.ThreadCancled.WaitOne(0) Then 'cancel is singalled apiThreadParamaters.success = False apiThreadParamaters.message = "The operation was canceled" Else 'imagine something is being processed Thread.Sleep(3000) apiThreadParamaters.success = GetQrResultsEnum.Fail apiThreadParamaters.QrString = Nothing apiThreadParamaters.message = "An unknown error has occured" End If Catch ex As Exception Finally apiThreadParamaters.ThreadCompleted.Set() End Try End Sub
code on the click even for the cancel button on the backgroundForm from the code above
Dim cancelOperationEvent As ManualResetEvent = m_CancelOperationEvent If (cancelOperationEvent IsNot Nothing) Then cancelOperationEvent.Set() SetMessage("waiting for operation to cancel...") End If
Thank you in advance for your assistance.
Liam