Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在带有BackgroundWorker的VB.Net中使用Console.Readkey方法时出现的问题_Vb.net_Console - Fatal编程技术网

在带有BackgroundWorker的VB.Net中使用Console.Readkey方法时出现的问题

在带有BackgroundWorker的VB.Net中使用Console.Readkey方法时出现的问题,vb.net,console,Vb.net,Console,我正试图用VB控制台来模拟康威的生活游戏(我很奇怪),一切都正常。该程序在四个循环中进行,每个循环具有单独的布尔条件:暂停、完成、清除和退出,从最内层到最外层。控制一切是一个后台工作人员,它读取按键并更改所有布尔变量。只有在使用BackgroundWorker和Console.Readkey的时候,我才发现需要两次才能读懂一个按键。我在其他项目中也有过,我不知道为什么。当我暂停程序时,它显然在等待两个线程上的按键,但只有当我第一次按下它时,调试器才会提示我进行按键。这不是什么大问题,但尽管如此,

我正试图用VB控制台来模拟康威的生活游戏(我很奇怪),一切都正常。该程序在四个循环中进行,每个循环具有单独的布尔条件:暂停、完成、清除和退出,从最内层到最外层。控制一切是一个后台工作人员,它读取按键并更改所有布尔变量。只有在使用BackgroundWorker和Console.Readkey的时候,我才发现需要两次才能读懂一个按键。我在其他项目中也有过,我不知道为什么。当我暂停程序时,它显然在等待两个线程上的按键,但只有当我第一次按下它时,调试器才会提示我进行按键。这不是什么大问题,但尽管如此,我还是想知道我能做些什么来消除这个问题

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

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.抹掉。

好吧,在我看来,第一个循环下面有“开始=错误:完成=错误:暂停=错误:退出=错误:迭代=错误:清除=错误”,所以它周围的每个循环都会重置

但我不太确定这样做是否可行,老实说,我经验不足,昏昏欲睡,而且渴望咖啡因

编辑:做了一些乱七八糟的事,我也有同样的问题,也在一个循环中,我会更深入地研究它

是的,通过在业余时间胡闹,我想出了一个不太方便的解决方案,让我测试一下,然后我会发布,只是想让sue知道这不是运气

是的,只是运气。由于我在控制台应用程序方面缺乏经验,我对命令的了解还不足以确定,但这可能是焦点问题