7

有没有办法在屏幕上没有 UI 的情况下做到这一点?目前,我正在让这个小型非 UI 程序每 60 秒从 3270 大型机读取和写入文本文件的信息,假设用户想在 60 秒等待的中间取消,我怎么能“听”一个没有来自 UI 的任何事件的按键?

4

1 回答 1

1

您需要某种界面来捕获击键。

这是在控制台应用程序中运行的示例(创建一个空白并粘贴到 defauklt 模块)

它允许在后台线程中处理“某事”,同时让 GUI 空闲以供用户输入命令。在这种情况下,只需一个简单的 1 秒延迟计数器即可达到 1000。

选项比较文本

Module Module1

Sub Main()
    Console.WriteLine("Enter ""Start"", ""Stop"", or ""Exit"".")
    Do
        Dim Com As String = Console.ReadLine
        Select Case Com
            Case "Start"
                Console.WriteLine(StartWork)
            Case "Stop"
                Console.WriteLine(StopWork)
            Case "Exit"
                Console.WriteLine("Quiting on completion")
                Exit Do
            Case Else
                Console.WriteLine("bad command Enter ""Start"", ""Stop"", or ""Exit"".")
        End Select
    Loop
End Sub

Public Function StartWork() As String
    If ThWork Is Nothing Then
        ThWork = New Threading.Thread(AddressOf Thread_Work)
        ThWork.IsBackground = False  'prevents killing the work if the user closes the window.
        CancelWork = False
        ThWork.Start()
        Return "Started Work"
    Else
        Return "Work Already Processing"
    End If
End Function

Public Function StopWork() As String
    CancelWork = True
    If ThWork Is Nothing Then
        Return "Work not currently running"
    Else
        Return "Sent Stop Request"
    End If
End Function

Public CancelWork As Boolean = False
Public ThWork As Threading.Thread = Nothing
Public dummyCounter As Integer = 0

Public Sub Thread_Work()
    Try
        Do
            dummyCounter += 1
            Console.Title = "Working ... #" & dummyCounter
            ' ###############
            ' do a SMALL PART OF YOUR WORK here to allow escape...
            ' ###############
            If dummyCounter >= 1000 Then
                Console.Title = "Work Done at #" & dummyCounter
                Exit Do
            ElseIf CancelWork Then
                Exit Do
            End If
            Threading.Thread.Sleep(1000) ' demo usage only.
        Loop
    Catch ex As Exception
        Console.WriteLine("Error Occured at #" & dummyCounter)
    End Try
    ThWork = Nothing
    If CancelWork Then
        Console.Title = "Work Stopped at #" & dummyCounter
    End If
End Sub

End Module
于 2013-06-03T22:16:40.507 回答