使用BackgroundWorker在VB.Net中使用Console.Readkey方法的问题

时间:2013-03-26 07:17:06

标签: vb.net console

我正在尝试使用VB控制台构建Conway的生命游戏模拟(我很奇怪。)事情有点工作。该程序在具有单独布尔条件的四个循环内发生:暂停,完成,清除和退出,从最里面到最外面。控制一切是BackgroundWorker,它读取Keypresses并更改所有布尔变量。只有在使用BackgroundWorker和Console.Readkey时,我才会在需要两次读取Keypress的情况下得到这个东西。我在其他项目中也有它,我无法弄清楚原因。当我暂停程序时,它显然正在等待两个线程上的按键,但只有当我第一次按下它时,调试器才会提示我按下按键。这不是一个大问题,但同样,我想知道我能做些什么来消除这个问题。

这是主要的子,不包括声明:

Do Until Quit = True 'This block repeats until Quit is true
        Start = False : Finish = False : Pause = False : Quit = False : Iterate = False : Clear = False
        Generation = 0 'This resets the Generation counter to 0
        ClearArray(Board) 'This resets a 2D boolean array to false.
        UpdateTitle(Generation) 'This updates the Console.Title to show the current generation.

        DrawBorder(Board, Offset, ConsoleColor.Cyan, BorderBlock) 'This draws an ASCII border around the grid.
        DrawBoard(Board, Offset) 'This draws the physical grid itself.

        WriteMessage(1, BoardDimensions(1) - 1, Offset) 'This writes a line below the grid prompting user input.
        ObtainInitialSetup(Board, Offset) 'This sub links to a bunch of other subs allowing the user to input an initial pattern.
        TransferArrayContents(Board, BoardMemory) 'This stores the contents of the pattern the user has entered into a memory array.
        Clear = False

        Do Until Clear = True 'This block repeats until Clear is true
            TransferArrayContents(BoardMemory, Board) 'This recalls the saved pattern from memory. If a user presses "Stop" then all generation
            'will cease and this is where the board is reset.
            DrawBoard(Board, Offset) 'The board is redrawn.
            WriteMessage(2, BoardDimensions(1) - 1, Offset)

            Do 'This block waits for the user to confirm before starting.
                Keypress = Console.ReadKey(True)
                If Keypress.Key = StartKey Then Start = True : Finish = False : Clear = False : Quit = False : Pause = False
                If Keypress.Key = QuitKey Then Finish = True : Clear = True : Quit = True
                If Keypress.Key = ClearKey Then Finish = True : Clear = True
                'The program does not start until a user presses Start, Clear or Quit.
            Loop Until Start = True Or Finish = True Or Clear = True Or Quit = True

            If Quit = False Then WriteMessage(3, BoardDimensions(1) - 1, Offset)
            If Finish = False Then If BK.IsBusy = False Then BK.RunWorkerAsync()
            'The backgroundworker is called so long as it isn't already running.

            Do Until Finish = True 'This block repeats until Stop is true
                Iterate = False 'The Iterate condition allows the Loop to go through once.
                UpdateTitle(Generation) 'This updates the title.
                Generate(Board, CoordList, Generation, Survivals, Births) 'This sub does the calculation across the whole Board and changes
                'the values in the board for the next generation.
                DrawBoard(Board, Offset) 'This draws the new board.
                CheckPause(Offset, BoardDimensions(1) - 1) 'This sub holds the loop if Pause is true by inserting a new Do Loop.
            Loop
        Loop
    Loop

    BK.CancelAsync()

这是BackgroundWorker Sub:

Sub ReadBackgroundKey() Handles BK.DoWork
    Dim Keypress As ConsoleKeyInfo
    Do
        Keypress = Console.ReadKey(True)
        If Keypress.Key = StopKey Then 'Finish
            Quit = False
            Clear = False
            Finish = True
        ElseIf Keypress.Key = PauseKey Then 'Pause the generation.
            Quit = False
            Clear = False
            Finish = False
            Pause = Not Pause
        ElseIf Keypress.Key = QuitKey Then 'Quit the game.
            Quit = True
            Clear = True
            Finish = True
            Pause = False
        ElseIf Keypress.Key = StepKey Then 'Step a generation.
            Quit = False
            Clear = False
            Finish = False
            Pause = True
            Iterate = True
        ElseIf Keypress.Key = ClearKey Then 'Clear the board.
            Quit = False
            Clear = True
            Finish = True
            Pause = False
        End If
    Loop Until Quit = True Or Clear = True
End Sub

另外,如果你注意到任何可敬的程序员都不应该做的事情,那么所有的批评都是受欢迎的 - 我只是做了半年的VB而且我还不是很好。

我感谢所有的想法和反馈:)。

编辑:另外,我不能在帖子的开头放上“你好”或任何愉快的问候语,它会被淘汰o.O。

1 个答案:

答案 0 :(得分:0)

好吧,它看起来像你有“Start = False:Finish = False:Pause = False:Quit = False:Iterate = False:Clear = False”在第一个循环中,所以它周围的每个循环都会重置。 / p>

不太确定是否会这样做,老实说,我经验不足,疯狂昏昏欲睡,渴望咖啡因。

编辑:有些搞乱它,我有同样的问题,也在一个循环内,将更多地看待它

是的,所以在闲暇时间乱搞,我想出了一个不那么方便的解决方案,让我测试一下然后我会发布,只是想起诉它不是运气。

是的,运气好。由于我对控制台应用程序缺乏经验,我不太清楚这些命令是否可以肯定,但这可能是一个焦点问题。