2008/05/20(Tue) 10:36:01 編集(投稿者)
■No19076 (シャノン さん) に返信
> (非同期読み取りは内部的にマルチスレッドなので)、こんなコードでできました。
どこまで考えるかによりますが、
ユーザーモードの範囲内なら非同期読み取りはマルチスレッドでないものもありますよ。
特にストリームは。
> 他にも方法はあるかと思います。
じゃあWindows.Formsで。
ただ、鶏唐揚さんが何をしたいのかいまいちよくわからないのですよね。
こんな簡単なのでいいのかしら?
Dim proc As Process
Dim ar As IAsyncResult
Dim dlg As ReadLine
Delegate Function ReadLine() As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If proc IsNot Nothing Then Exit Sub
proc = New Process
With proc.StartInfo
.FileName = "PING"
.Arguments = "192.168.68.1" '私のプライベートアドレスはこれ。
.RedirectStandardOutput = True
.WindowStyle = ProcessWindowStyle.Hidden
.CreateNoWindow = True
.UseShellExecute = False
End With
proc.Start()
dlg = New ReadLine(AddressOf proc.StandardOutput.ReadLine)
ar = dlg.BeginInvoke(Nothing, Nothing)
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If ar.IsCompleted Then
Dim str As String
str = dlg.EndInvoke(ar)
If str Is Nothing Then
Timer1.Stop()
proc = Nothing
Else
Me.TextBox1.Text &= str & ControlChars.NewLine
ar = dlg.BeginInvoke(Nothing, Nothing)
End If
End If
End Sub
追記。
> Startで固まるというのは、PINGコマンドが完了するまでメインスレッドが待機して
> しまうということです。WaitForExitを外しても同様でした。
StartではなくReadToEndで固まってるのではないですか?
> コマンドプロンプトで単純に
> PING xxx.xxx.xx.xx
> と打つと、(PINGが通る場合は)通常は約1秒ごとにReply from...と
> 4回表示されると思いますが、これをそのままのタイミングで
> 標準出力を奪って表示したいということです。
とすると、ストリームの扱いに慣れていないのでしょう。
No18620 の投稿とかにちょっと書いたので参考に。