2013-06-03 8 views
6

C'è un modo per farlo senza interfaccia utente sullo schermo? Attualmente sto facendo in modo che questo piccolo programma non-UI legga e scriva le informazioni da un mainframe 3270 ad un file di testo ogni 60 secondi, supponiamo che l'utente voglia cancellare nel bel mezzo dell'attesa di 60 secondi, come potrei "ascoltare" per un keypress senza eventi dall'interfaccia utente?'Listen' per pressione di un tasto mentre il thread sta dormendo

risposta

1

è necessario un qualche tipo di interfaccia per catturare le battiture.

Ecco ad esempio che viene eseguito in un'applicazione console (crearne uno vuoto e incollare nel modulo defauklt)

Permette l'elaborazione "qualcosa" in un thread in background, lasciando l'inattività GUI per l'utente di immettere comandi . In questo caso solo un semplice contatore 1 secondo di ritardo al 1000.

Option Compare Text

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 
+0

si potrebbe desiderare di chiamare stopwork quando si esce – user1937198

+0

era solo codice di esempio semplice e si dovrebbe adattare ad ogni caso specifico. nella maggior parte dei casi, l'elaborazione dei dati in cui si esce dalla GUI dell'applicazione, vorrei che finisse il suo lavoro, a meno che non l'avessi detto esplicitamente. – DarrenMB

Problemi correlati