473,396 Members | 2,002 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,396 software developers and data experts.

HOWTO Make StreamReader class not block.

ATS
HOWTO Make StreamReader class not block.

Please help,

I'm trying to use the Std-Out from a process I launch, to read back data
from the process, until either the data I expcet is passed, or a time limit
hgas expired. My problem is that the StreamReader class blocks until the
process writes something. Here is a snippet of code with the problem:

Process_MyProcess = new System.Diagnostics.Process();
ProcessStartInfo_MyProcess = new
System.Diagnostics.ProcessStartInfo();

ProcessStartInfo_MyProcess.FileName = "My Path";
ProcessStartInfo_MyProcess.RedirectStandardInput = true;
ProcessStartInfo_MyProcess.RedirectStandardOutput = true;
ProcessStartInfo_MyProcess.UseShellExecute = false;

Process_MyProcess.StartInfo = ProcessStartInfo_MyProcess;

if (!Process_MyProcess.Start())
{
throw(new Exception("Error"));
}

StreamWriter_StdIn = Process_MyProcess.StandardInput;
StreamWriter_StdIn.Write(csStdIn);

StreamReader_StdOut = Process_MyProcess.StandardOutput;

int iChar, iTotalTimesInSeconds = 0;
DateTime DateTime_Start = DateTime.Now;
TimeSpan TimeSpan_Temp;

// ERROR!
//
// I want to use ReadLine, but that blocks, Even Peek as shown below
blocks...
//
// what can I do?

for
(
TimeSpan_Temp = DateTime.Now - DateTime_Start,
iChar = StreamReader_StdOut.Peek(); <== This blocks!!!!!
((iChar < 0) && (TimeSpan_Temp.TotalSeconds < 30));
TimeSpan_Temp = DateTime.Now - DateTime_Start,
iChar = StreamReader_StdOut.Peek()
)
{
System.Threading.Thread.Sleep(100);
}

Nov 17 '05 #1
1 1997
ATS <AT*@discussions.microsoft.com> wrote:
Thanks for the reply, but I figured it out. I used the
SteamReader.BaseStream.BeginRead to make it work. Thanks anyways.


Sorry, I assumed you wanted to actually use the functionality of the
StreamReader - just reading the binary data from the stream doesn't do
that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

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

Similar topics

4
by: Rush | last post by:
If i create a new StreamReader object, based on a filename that does not exist, the SR correctly raises an exception that the file doesn't exist. Great! I love it. BUT....then SR proceeds to...
9
by: oafyuf | last post by:
Hi, I'm having performanbce issues with StreamReader and was wondering what I could do to improve it... The following takes around 3 seconds to process! The content of the response is: ...
4
by: Astronomically Confused | last post by:
using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; class HttpProcessor { private Socket s;
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
0
by: Mark Harrison | last post by:
HOWTO: Integrating Posgresql queries into an event loop. Mark Harrison mh@pixar.com May 27, 2004 Problem ------- The commonly used postgresql APIs will block until completed.
0
by: Benjamin | last post by:
I am attempting to create multiple itterators for a custom class that I have created. Basically I have a class that I am populating using XML de-serialization. I want to be able to loop through the...
1
by: Dave Booker | last post by:
I am reading a text file using StreamReader. If while I have that stream open I open the underlying text file in, say, Excel, Excel will announce that the file is locked for reading and will offer...
6
by: Pieter | last post by:
Hi, For some procedures that throws exceptions, I would like to show different messages to the user depending on what type of exception he's getting. For instance this one: when the file is...
7
by: Eran.Yasso | last post by:
Hi, In the MSDN the sample doesn't use the close() method. But I know that in most languages you do need to use the close() method after reading and writing to a file. from MSDN: public...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.