-
.net/Process buffer question
Hi everyone,
I have a problem when using Process in vb.net. In my code (as you see below) I redirect the input and output streams and then run an r.exe (a statistical data analysis package). Then I issue few commands to r and read the output in a string variable. This works till I read the output from r. When I attempt to read beyond 1024 character from the standard output the program hangs-up. Anyone have an idea why is that?
Thanks
-----output ------------------------------------
<h3>CTY2003core</h3>
<center><h4>c###e---Approximate annual cost/rate of interest of loan</h4>
by size---Size [C]
</center>
<center><table border=1>
<tr ID=header>
<td>size</td><td>Count</td><td>Mean</td><td>Std Err</td> <td>nearMin</td><td>nearMax</td></tr>
<tr valign=top><td id=header>Micro</td><td><a onclick=help('protected.htm') onmouseover=this.style.cursor='hand'>Protected</a></td><td> </td><td> </td><td> </td><td> </td><td></tr>
<tr valign=top><td id=header>Small</td><td>162</td><td>77.86598</td><td>235.4409</td><td>6.167781</td><td>181.2665</td><td></tr>
<tr valign=top><td id=header>Medium</td><td>65</td><td>3445.878</td><td>27435.10</td><td>7.442417</td><td>101.2197</td><td></tr>
<tr valign=top><td id=header>Large</td><td>57</td><td>36.66272</td><td>24.71664</td><td>6.167781</td><td>79.58563</td><td></tr>
<tr valign=top><td id=header>Very large</td><td>35</td><td>3356.073</td><td>19528.05</td><td>8.731066</td><td>112.9096</td><td></tr>
</table></center>
-----code ------------------------------------
Namespace MyProcessSample
Public Class xMyProcess
Public Shared Sub Main()
Dim myProcess As Process = New Process
Dim e As String
Dim s As String
'---- setting myProcess and redirecting standard I/O
myProcess.StartInfo.FileName = "C:\Program Files\R\rw2010\bin\R.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.StartInfo.Arguments() = "--slave --silent --vanilla"
myProcess.Start()
Dim sIn As StreamWriter = myProcess.StandardInput
Dim sOut As StreamReader = myProcess.StandardOutput
'---- this code is specific to r.exe
'---- this will set some parameters and then run r scripts
'---- the our put is an html table with a header
sIn.AutoFlush = True
sIn.WriteLine("setwd('C:/Documents and Settings/Suehail/My Documents/R/tomcat/webapps/ics/'); " & System.Environment.NewLine)
sIn.WriteLine("source('WEB-INF/R/common.r'); " & System.Environment.NewLine)
sIn.WriteLine("source('WEB-INF/R/xTab2.r'); " & System.Environment.NewLine)
sIn.WriteLine("filterVar<-''; " & System.Environment.NewLine)
sIn.WriteLine("elist<-'c229e'; " & System.Environment.NewLine)
sIn.WriteLine("llist<-'Approximate annual cost/rate of interest of loan'; " & System.Environment.NewLine)
sIn.WriteLine("elist[2]<-'size'; " & System.Environment.NewLine)
sIn.WriteLine("llist[2]<-'Size [C]'; " & System.Environment.NewLine)
sIn.WriteLine("qRead('Brazil2003core', c229e,size); " & System.Environment.NewLine)
sIn.WriteLine("makeVars(elist,''); " & System.Environment.NewLine)
sIn.WriteLine("makeDB(2); " & System.Environment.NewLine)
sIn.WriteLine("xTab2(5,'c841625272'); " & System.Environment.NewLine)
sIn.WriteLine("finish(); " & System.Environment.NewLine)
'---- end of r.exe code
Dim ss As String = ""
Dim zz As Long
zz = 0
'---- reading output from r.exe. this will hang-up when reading the 1024th char
Do While sOut.Peek <> -1
ss = ss + Convert.ToChar(sOut.Read())
zz = ss.Length
Loop
If Not myProcess.HasExited Then
myProcess.Kill()
End If
sIn.Close()
sOut.Close()
myProcess.Close()
End Sub
End Class
End Namespace
-
It is your choice of StreamWriter (I guess)
Is the following your problem?
Do While sOut.Peek <> -1
ss = ss + Convert.ToChar(sOut.Read())
zz = ss.Length
Loop
Well, ss is a System.String and so on.
I recommend you use a standard text file.
Maybe, since the output looks like html,
you can save this file with a .html extension.
In a few words, don't use ss as string as sOut as StreamWriter.
Save this standard output to a text file, and give it an .html extension.
Tonci.
-
.net/Process buffer question
You seem to be redirecting standard error but you never read from it. This can cause the process to block. Normally this is done in another thread but I believe you can use Peek() to do it in the same thread although in that case you must use Read() and never ReadLine() or ReadToEnd() as these can result in blocks.
------------------------
The Low GI Diet
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks