Остановите backgroundworker из другой подпрограммы
I am using a backgroundworker in vb 2012 as a game timer. The DoWork routine increments a Integer representing a "seconds elapsed" value in a FOR loop. With each execution of the loop, it checks to see if CancellationPending = True. Additionally, within the loop, I call another sub that checks to see if the player has correctly matched all of the displayed playing cards. If so, I want to STOP the timer, reset some necessary variables, and reset the cards for the next round. The Backgroundworker WILL NOT stop from the other sub. I know this topic has been covered before, but I just don't understand what I need to do. Here is my code:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork For tmpInt1 As Integer = 1 To TimerVal If BackgroundWorker1.CancellationPending = True Then e.Cancel = True Exit For Else Chk_All_Matches() End If If Sounds = True Then If ATT = "CM" And SoundMatch = True Then My.Computer.Audio.Play(SoundsPath & "match.wav") If SoundTick = True Then My.Computer.Audio.Play(SoundsPath & "tick.wav") End If tmpInt2 = tmpInt2 + 1 If tmpInt2 = 60 Then ElapsedMin = ElapsedMin + 1 tmpInt2 = 0 End If Label3.Text = "Elapsed Time: " & Format(ElapsedMin, "0#") & Format(tmpInt2, ":0#") System.Threading.Thread.Sleep(1000) 'stop after advancing one integer for 1 second. Next End Sub Private Sub Chk_All_Matches() If NumMatches = 26 And MatchNumbers = True And NumRounds = 1 Then BackgroundWorker1.CancelAsync() MessageBox.Show("CONGRATULATIONS! You matched them all!", "GAME OVER", MessageBoxButtons.OK, MessageBoxIcon.Stop) Stop_Game() Exit Sub End If If NumMatches = 26 And MatchNumbers = True And NumRounds > 1 Then BackgroundWorker1.CancelAsync() MessageBox.Show("CONGRATULATIONS! You matched them all in this round!" & Chr(10) & "Click OK to start the next round.", "ROUND COMPLETED", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) StartGame = False RoundNumber = RoundNumber + 1 NumClicks = 0 NumMatches = 0 Label6.Text = "Round #: " & Str(RoundNumber) Label1.Text = "# of Matches:" tmpInt2 = 0 ElapsedMin = 0 ElapsedSec = 0 Show_Board() Generate_Card_Order() Show_Board() StartGame = True BackgroundWorker1.RunWorkerAsync() End If If NumMatches = 24 And MatchSuits = True And NumRounds = 1 Then BackgroundWorker1.CancelAsync() MessageBox.Show("CONGRATULATIONS! You matched them all!", "GAME OVER", MessageBoxButtons.OK, MessageBoxIcon.Stop) Stop_Game() Exit Sub End If If NumMatches = 24 And MatchSuits = True And NumRounds > 1 Then BackgroundWorker1.CancelAsync() MessageBox.Show("CONGRATULATIONS! You matched them all in this round!" & Chr(10) & "Click OK to start the next round.", "ROUND COMPLETED", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) StartGame = False RoundNumber = RoundNumber + 1 NumClicks = 0 NumMatches = 0 Label6.Text = "Round #: " & Str(RoundNumber) Label1.Text = "# of Matches:" tmpInt2 = 0 ElapsedMin = 0 ElapsedSec = 0 Show_Board() Generate_Card_Order() StartGame = True BackgroundWorker1.RunWorkerAsync() End If End Sub
Что я уже пробовал:
Опуская команды "BackgroundWorker1.CancelAsync()" и просто позволяя коду работать, но таймер продолжает отображаться, и я этого не хочу!