有没有办法在屏幕上没有UI的情况下执行此操作?目前我有这个小的非UI程序每隔60秒从3270大型机读取和写入信息到文本文件,假设用户想要在60秒等待的中间取消,我怎么能'听'为没有来自UI的任何事件的按键?
答案 0 :(得分:1)
你需要某种界面来捕捉击键。
以下是在控制台应用程序中运行的示例(创建一个空白的并粘贴到defauklt模块中)
它允许在后台线程中处理“SOMETHING”,同时让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