gpt4 book ai didi

vb.net - 在线程休眠时“监听”按键

转载 作者:行者123 更新时间:2023-12-04 12:54:54 24 4
gpt4 key购买 nike

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

最佳答案

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

这是在控制台应用程序中运行的示例(创建一个空白并粘贴到 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

关于vb.net - 在线程休眠时“监听”按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com