16/03/11 19:59
Brenza
Ciao a tutti, sto creando un programma in vb.net e sono incappato in un problema che proprio non riesco a risolvere.
Nel form 1 ho dichiarato una TextBox e un Process, il mio obbiettivo è quello di lanciare un processo esterno (ping.exe) e di memorizzarne in in real-time l'output all'interno della textbox.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Private Sub Process1_OutputDataReceived(ByVal sender As System.Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles Process1.OutputDataReceived
If Not e.Data Is Nothing Then TextBox1.text += e.Data.ToString
End Sub
Per evitare il Cross-thread Exeption ho implementato la sub SetText per modificare la TextBox
Il problema è che l'istruzione
Solutions?
EDIT: Ho pensato a backgroundworking e a streamreader ma non riesco a fare quello che voglio.
Nel form 1 ho dichiarato una TextBox e un Process, il mio obbiettivo è quello di lanciare un processo esterno (ping.exe) e di memorizzarne in in real-time l'output all'interno della textbox.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
startInfo.FileName = Environ("WINDIR") & "\system32\ping.exe" startInfo.Arguments = "127.0.0.1" startInfo.CreateNoWindow = True startInfo.UseShellExecute = False startInfo.RedirectStandardOutput = True Process1 = Process.Start(startInfo) Process1.BeginOutputReadLine() Process1.WaitForExit()
Private Sub Process1_OutputDataReceived(ByVal sender As System.Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles Process1.OutputDataReceived
If Not e.Data Is Nothing Then TextBox1.text += e.Data.ToString
End Sub
Per evitare il Cross-thread Exeption ho implementato la sub SetText per modificare la TextBox
Private Sub SetText(ByVal [text] As String) if Me.TB.InvokeRequired Then Dim d As New SetTextCallback(AddressOf SetText) Me.Invoke(d, New Object() {[text]}) Else : TextBox1.Text += [text] & vbCrLf End If End Sub Delegate Sub SetTextCallback(ByVal [text] As String)
Il problema è che l'istruzione
Process1.WaitForExit()blocca il funzionamento dell'intero programma (l'istruzione va in contrasto con quella per leggere l'output), ne ho assoluamente bisogno in quanto ho in programma di eseguire diversi processi uno dopo l'altro assicurandomi che non coesistano mai.
Solutions?
EDIT: Ho pensato a backgroundworking e a streamreader ma non riesco a fare quello che voglio.
Ultima modifica effettuata da Brenza 16/03/11 20:06
aaa