从另一个子例程中停止后台工作 [英] Stop a backgroundworker from another subroutine

查看:74
本文介绍了从另一个子例程中停止后台工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vb 2012中使用背景工作者作为游戏计时器。 DoWork例程递增表示FOR循环中秒已过去值的整数。每次执行循环时,它会检查CancellationPending = True。另外,在循环中,我调用另一个子,它检查玩家是否正确匹配了所有显示的扑克牌。如果是这样,我想停止计时器,重置一些必要的变量,并重置下一轮的卡。 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()命令并让代码运行,但计时器继续显示我不希望这样!



What I have tried:

Omitting the "BackgroundWorker1.CancelAsync()" commands and just letting the code run, but the timer continues to display and I don't want that!

推荐答案

BackgroundWorker(BGW)不适合一个游戏的模式;你最好使用一个定时器(例如)每秒30帧并执行帧更新代码;而不是让BGW尝试管理自己(严重)。
BackgroundWorker (BGW) is not a suitable "pattern" for a game; you are better off using a Timer that fires at (say) "30 frames per second" and executes "frame update code"; instead of having the BGW try and "manage itself" (badly).


我要感谢你为我澄清这一点。我最初使用过Timer,直到我看到另一个使用Backgroundworker的网站。我决定使用它,因为我也试图播放声音,当使用计时器时,声音与事件不匹配。那好吧!非常感谢您的帮助!
I want to THANK YOU for clarifying that for me. I originally had used a Timer until I saw another website on using the Backgroundworker. I decided to use that because I'm also trying to play sounds, when using a timer, the sounds don't match up with the event. Oh well! Thank you VERY sincerely for your help!


这篇关于从另一个子例程中停止后台工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆