We have answer of your question!

100% solved queries, no empty question

Question: 'Listen' for a keypress while thread is sleeping


Is there a way to do this with no UI on the screen? Currently I'm having this small non-UI program read and write information from a 3270 mainframe to a Text File every 60 seconds, suppose the user wants to cancel in the middle of the 60 second wait, how could I 'listen' for a keypress without any events from the UI?

Question author Criel | Source



You need some kind of interface to capture keystrokes.

Here is example that runs in a console application (create a blank one and paste into the defauklt module)

It allows processing "SOMETHING" in a background thread while leaving the GUI idle for the user to enter commands. In this case just a simple 1 second delay counter to 1000.

Option Compare Text

Module Module1

Sub Main()
    Console.WriteLine("Enter ""Start"", ""Stop"", or ""Exit"".")
        Dim Com As String = Console.ReadLine
        Select Case Com
            Case "Start"
            Case "Stop"
            Case "Exit"
                Console.WriteLine("Quiting on completion")
                Exit Do
            Case Else
                Console.WriteLine("bad command Enter ""Start"", ""Stop"", or ""Exit"".")
        End Select
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
        Return "Started Work"
        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"
        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()
            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.
    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
Answer author Darrenmb

Tickanswer.com is providing the only single recommended solution of the question 'Listen' for a keypress while thread is sleeping under the categories i.e vb.net , . Our team of experts filter the best solution for you.

Related Search Queries:

You may also add your answer

Thanks for contributing an answer to Tick Answer!