Strange behavour of Process

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!
Working with an VB.NET app that calls a lot of 3:rd party programs via the
Process class. All calls are made with this pattern:

Dim p as New Process
p.StartInfo.FileName = <full path to the exe-file>
p.StartInfo.Arguments = <arguments to the exe>
p.StartInfo.CreateNoWindow = True
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardError = True
p.StartInfo.RedirectStandardOutput = True
 
Sorry. I slipped on the keyboard. Here is the complete question:

Hi!
Working with an VB.NET app that calls a lot of 3:rd party console programs
via the
Process class. All calls are made with this pattern:

Dim p as New Process()
p.StartInfo.FileName = <full path to the exe-file>
p.StartInfo.Arguments = <arguments to the app>
p.StartInfo.CreateNoWindow = True
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardError = True
p.StartInfo.RedirectStandardOutput = True
p.start()
p.WaitForExit()
Dim sr As StreamReader = p.StandardOutput
Dim stdOut As String = sr.ReadToEnd()
sr.Close()
p.Close()

This works fine for all exe-files EXCEPT ONE! Strange... Then I tried to
figure out why it doesn't work. But this really confuses me..

I changed to using the shell instead:

Dim p as New Process()
p.StartInfo.FileName = <full path to the exe-file>
p.StartInfo.Arguments = <arguments to the app>
p.StartInfo.UseShellExecute = True
p.start()
p.WaitForExit()
p.Close()

This works (as well as when i run exactly the same command in a console).
But I don't want the console window to pop up so therefore I add the
following line:

p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

Now it doesn't work again!!! This is really true, it only works when the
console window is visible!!!
It must have something to do with the program I'm calling but also isn't
this strange by Process?
The program I call produces about 50 lines of output, takes about 15 seconds
to execute and creates a new file (which name is given as an argument). It
really takes 15 seconds in both cases when it works or not.
But the output (= the file) is only created when the console is visible.

I would be very glad if anyone has any clue to this.
 
Hi

Did the all the EXE are of Console Application?
Have you tried to write a test console application for a test to see if
that reproduce the problem?

Also from the document, it seems that we need to set the UseShellExecute to
false to redirect the output stream.
Setting this property to false enables you to redirect input, output, and
error streams.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdiagnosticsprocessstartinfoclassuseshellexecutetopic.asp

Commonly, if your code works for all the other EXE, (I assume they are all
Console Application), I guess maybe the problem EXE file has do something
expecially.
I think you may also try to contact the EXE's develop for the source code,
so that we can check what is the problem.

Also I think you may try to call the CreateProcess directly to see if that
works for you.

Here are some articles for your reference.

How to spawn a process that runs under the context of the impersonated user
in Microsoft ASP.NET pages
http://support.microsoft.com/kb/889251

Desktop Switching
http://www.thecodeproject.com/csharp/CsDesktopSwitching.asp

If I have any misunderstanding, please feel free to post here.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi
Yes, all exe are console apps that I think are written in Microsoft C (not
plusplus).
Yes, UseShellExecute was False and that didn't work for this exe so
therefore I changed to True to see if that worked.
No, I didn't make a test console application because I imagined no success
with that. But now I've written a test. But unfortunately this doesn't solve
my problem, but I discovered an important issue. It seems that the standard
output of a console application is limited to 2 kB. If the exe produces more
than that it will hang until the output is emptyed.

Try this:

Console app=
Module Module1
Sub Main(ByVal args() As String)
Dim filename As String = args(0)
Dim fi As New FileInfo(filename)
If (fi.Exists) Then
Dim sr As New StreamReader(filename)
Dim line As String
Do
line = sr.ReadLine()
If (Not (line Is Nothing)) Then
Console.WriteLine(line)
End If
Loop Until line Is Nothing
sr.Close()
End If
End Sub
End Module

Windows app:
(behind a button)
Try
p = New Process
p.StartInfo.FileName = <full path to console exe>
p.StartInfo.Arguments = <name of a text file>
p.StartInfo.CreateNoWindow = True
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardError = True
p.StartInfo.RedirectStandardOutput = True
p.Start()

p.WaitForExit()
Dim sr As StreamReader = p.StandardOutput
Dim stdOut As String = sr.ReadToEnd()
Trace.Writeline("stdout = " & ControlChars.CrLf & stdOut)
sr.Close()
sr = p.StandardError
Dim stderr As String = sr.ReadToEnd()
Trace.Writeline("stdErr = " & ControlChars.CrLf & stderr)
sr.Close()
p.Close()
Catch ex As Exception
Trace.Writeline(ex.GetType().ToString() & ": " & ex.Message)
End Try

When running this with a small text file it works. Then change to a file
more than 2 kB. The console app will hang. If you kill it the windows app
will read the first 2 kb only.

BUT, if we immediately after the p.Start() and before p.WaitForExit() reads
the output it works.
The same result appear if we run with UseShellExecute=True. As long as the
console window is displayed it won't get full. But if hidden it will be.
Seems logical.

BUT, unforunately these modifications doesn't solve my original problem.;-(

Maybe it is important to inform you that I'm running on W2K SP4.

Any more suggestions or hints?
 
Hi

Thanks for you trying.
We guess the "problem" Console app have some specially handling in the
standard output.

So far to isolate the problem, I think you may try to write a C++
application which will call the CreateProcess to see if that works.
So that we will know if the problem lie in the C# Process class or the
"problem" Console App itself.

BTW:
Did you have the source code?

You may have a try and let me know the result.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Your process may be blocking on the redirected standard output. Take a
look a the examples on this page:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemdiagnosticsprocessstartinfoclassredirectstandardoutputtopic.asp>

Katy

From: "=?Utf-8?B?TGFyc0o=?=" <[email protected]>
|
| Hi!
| Working with an VB.NET app that calls a lot of 3:rd party console
programs
| via the
| Process class. All calls are made with this pattern:
|
| Dim p as New Process()
| p.StartInfo.FileName = <full path to the exe-file>
| p.StartInfo.Arguments = <arguments to the app>
| p.StartInfo.CreateNoWindow = True
| p.StartInfo.UseShellExecute = False
| p.StartInfo.RedirectStandardError = True
| p.StartInfo.RedirectStandardOutput = True
| p.start()
| p.WaitForExit()
| Dim sr As StreamReader = p.StandardOutput
| Dim stdOut As String = sr.ReadToEnd()
| sr.Close()
| p.Close()
|
| This works fine for all exe-files EXCEPT ONE! Strange... Then I tried to
| figure out why it doesn't work. But this really confuses me..
|
| I changed to using the shell instead:
|
| Dim p as New Process()
| p.StartInfo.FileName = <full path to the exe-file>
| p.StartInfo.Arguments = <arguments to the app>
| p.StartInfo.UseShellExecute = True
| p.start()
| p.WaitForExit()
| p.Close()
|
| This works (as well as when i run exactly the same command in a console).
| But I don't want the console window to pop up so therefore I add the
| following line:
|
| p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
|
| Now it doesn't work again!!! This is really true, it only works when the
| console window is visible!!!
| It must have something to do with the program I'm calling but also isn't
| this strange by Process?
| The program I call produces about 50 lines of output, takes about 15
seconds
| to execute and creates a new file (which name is given as an argument).
It
| really takes 15 seconds in both cases when it works or not.
| But the output (= the file) is only created when the console is visible.
|
| I would be very glad if anyone has any clue to this.
|
| --
| Regards,
| Lars Jönson
|
|
 
Back
Top