473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

HOWTO Read from stdout without blocking.

ATS
HOWTO Read from stdout without blocking.

Please help,

I have an app that is launching another process, where it is letting the new
process inherit the STD-IN/OUT hanldes. The goal is to let the C# app use
STDOUT to get feedback from the app after it starts. The problem is that the
reading of STDOUT is blocking when the "EndRead" is called as such:

============================================
public System.Diagnostics.Process Process_MyApp = null;

// Process_MyApp is initialized...

public string ReadStdOut(int iLen, int iMaxTime)
{
if (StreamReader_StdOut == null)
{
ERR =
"The STD-OUT stream is not active at the moment and can not be read at
this time";
throw(new Exception(ERR));
}

DateTime DateTime_Start = DateTime.Now;
TimeSpan TimeSpan_Temp;
byte[] zbyteTemp = new byte[1];

System.IAsyncResult IAsyncResult_Temp;
string csBYTE;
string csRET = "";

for
(
TimeSpan_Temp = DateTime.Now - DateTime_Start,
IAsyncResult_Temp = StreamReader_StdOut.BaseStream.BeginRead
(
zbyteTemp, 0, 1, null, null
);
((iMaxTime > 0) && (TimeSpan_Temp.TotalSeconds < iMaxTime)) &&
!Process_MyApp.HasExited;
TimeSpan_Temp = DateTime.Now - DateTime_Start,
IAsyncResult_Temp = StreamReader_StdOut.BaseStream.BeginRead
(
zbyteTemp, 0, 1, null, null
)
)
{
StreamReader_StdOut.BaseStream.EndRead(IAsyncResul t_Temp); // This
blocks!!!!

if (IAsyncResult_Temp.IsCompleted)
{
csBYTE = "";
csBYTE += Convert.ToChar(zbyteTemp[0]);
csRET += csBYTE;

if (iLen > 0)
{
if (csRET.Length >= iLen)
{
break;
}
}
else
{
if (csBYTE == "\n")
{
break;
}
}
}

System.Threading.Thread.Sleep(50);
}

if ((iMaxTime > 0) && (TimeSpan_Temp.TotalSeconds > iMaxTime))
{
ERR =
"MyApp failed to start and return the expect RET code within " +
iMaxTime.ToString() + " " +
"seconds. MyApp may be hung and/or corrupt, and may require one to
manually " +
"stop the MyApp's process via the task manager";
throw(new Exception(ERR));
}

if (Process_MyApp.HasExited)
{
throw(new Exception("MyApp object has exited"));
}

return csRET;
}

Nov 17 '05 #1
2 3893
>>StreamReader_StdOut.BaseStream.EndRead(IAsyncRes ult_Temp); // This
blocks!!!!

I suggest you try the followings:
- Call Process.WaitForExit() before the blocking line above
- Pass an AsyncCallback for BeginRead, and call EndRead in that
AsyncCallback implementation.

Hope it helps,
Thi

Nov 17 '05 #2
ATS
Thanks for the reply, but that does not help. At any rate, I've given up on
this approach altogether.
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Arnau Sánchez | last post by:
Hello, I have a problem when reading from stdin (using it as pipe) in a Python program (the sender is written in C): C (sender) write(1, buffer, 4) Python (recv.py) sys.stdin.read(256) ...
5
by: Jeff Learman | last post by:
I want to do a very simple thing in Windows. (Using Python Shell.) I want to write a prompt to sys.stdout and read the user input. (Ideally, without waiting for a newline.) Here are the...
1
by: Jacek Pop³awski | last post by:
Popen from subprocess module gives me access to stdout, so I can read it. Problem is, that I don't know how much data is available... How can I read it without blocking my program? example:...
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
3
by: Phoe6 | last post by:
Hi all, I had a filesystem crash and when I retrieved the data back the files had random names without extension. I decided to write a script to determine the file extension and create a newfile...
6
by: placid | last post by:
Hi all, I have been looking into non-blocking read (readline) operations on PIPES on windows XP and there seems to be no way of doing this. Ive read that you could use a Thread to read from the...
8
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2...
4
by: rdabane | last post by:
Hi, I'm trying to perform following operation from inside the python script 1. Open a shell ( start a process ) 2. Send command1 to the process 3. Get output from the process 4. Send command2...
5
by: Thomas Christensen | last post by:
This issue has been raised a couple of times I am sure. But I have yet to find a satisfying answer. I am reading from a subprocess and this subprocess sometimes hang, in which case a call to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.