Executing thread in correct context



My ultimate goal is to get something like "ping.exe" to re-direct it's
standardOutput in real time to an .aspx page.

I've been able to get this behavior to work just fine in a regular
console application. I execute the process and the output is re-
directed to a windows textbox.

Below is the code I'm using to do that:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click


End Sub

Public Sub executeTest()

Dim p As New System.Diagnostics.Process()

p.StartInfo.FileName = "ping"
p.StartInfo.Arguments = "www.yahoo.com"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True

AddHandler p.OutputDataReceived, AddressOf outputHandler





End Sub

Public Delegate Sub AddTextCallback(ByVal strText As String)

Private Sub AddText(ByVal strText As String)

TextBox1.Text = TextBox1.Text & strText & Environment.NewLine

End Sub

Private Sub outputHandler(ByVal sendingProcess As Object, _
ByVal outLine As System.Diagnostics.DataReceivedEventArgs)

' Collect the sort command output.
If Not String.IsNullOrEmpty(outLine.Data) Then
Button1.Invoke(New AddTextCallback(AddressOf AddText), New
Object() {outLine.Data})
End If

End Sub

The key to code above for me was the "Button1.Invoke(...)" line.

On my .aspx page I have a webcontrol called "div1" that represents a
<div>. There is no such method called "Invoke" for this control.
Conceptually is there a way to invoke the delegate so that it runs in
the correct UI thread (whatever that thread is called)?

I've been reading like mad over the past few days about threads,
delegates, etc. so forgive my ignorance with terms etc. I've thought
also about creating a webservice to do this, but I'm not sure how to
return results in real time via the webservice. I can kick off the
process, but am not sure how to report the results back.



Alvin Bruney [MVP]

html controls or server controls are not windows controls so they do not
*suffer* the threading issues inherent in windows controls; you won't find
invoke on them because they are implicitly thread safe. (not quite true,
they are more thread-unaware).

When you execute the thread, you will need to go *scrape* the output window,
parse the text and store it in a variable. From that point, it will be
available to server code assuming that you invoked the output window from
the server code.

It does seem that you are doing it the other way around, that is, given a
command you are trying to write to a page. That's not possible, since you
don't have a call context.

You can write a webservice and return the data from a web method, then call
this webservice from the web page.

Alvin Bruney
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
Professional VSTO 2005 - Wrox/Wiley

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads