473,769 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I overcome the buffer limitation using StreamReader.Re adTo

Hi, I'm using VS 2005, ,.net 2 for C# windows application. I'm using Process
to run a C application and redirecting its standard output so I can read it
with StreamReader.Re adToEnd. It's only reading the 1st 5 lines or so when
there is about 30 lines of data.
I also tried using the Process.BeginOu tputReadlin and a
DataReceivedEvn tHandler to gather output but I also only get about 5 lines of
the data.
There seems to be a buffer size issue. Can someone tell me how can I
resolve this problem. Thank you.
--
Thanks.
Aug 3 '07 #1
11 9244
Pucca <Pu***@discussi ons.microsoft.c omwrote:
Hi, I'm using VS 2005, ,.net 2 for C# windows application. I'm using Process
to run a C application and redirecting its standard output so I can read it
with StreamReader.Re adToEnd. It's only reading the 1st 5 lines or so when
there is about 30 lines of data.
I also tried using the Process.BeginOu tputReadlin and a
DataReceivedEvn tHandler to gather output but I also only get about 5 lines of
the data.
There seems to be a buffer size issue. Can someone tell me how can I
resolve this problem. Thank you.
It should work fine. Do you know if your C application is actually
finishing? You should read from standard error as well as standard out
- otherwise if it's trying to write to standard error, it may be
blocking.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 3 '07 #2
Here is my code on first try to read the standard output. In the debug mode
I can see some lines of the data are read in but the process never has a
exited status. I even took away the time out value and it just hangs and
sits there and I can see the stream buffer hits a limit and hangs.
listFiles.WaitF orExit();
Is there a way to read all my data in without a buffer limitation? Thank you.

public static int GetNisFile(Syst em.Diagnostics. ProcessStartInf o psi,
ref DataTable dtAccounts,
ref ToolStripStatus Label slMain, ref StatusStrip ssMain, string
userOrGroup)
{

string accountFile="", output="";
int numOfAccounts = 0;
System.IO.Strea mReader fileOutput = null, myOutput = null;
System.Diagnost ics.Process listFiles;
try
{
listFiles = System.Diagnost ics.Process.Sta rt(psi);
if (listFiles != null)
{
myOutput = listFiles.Stand ardError;
fileOutput = listFiles.Stand ardOutput;
listFiles.WaitF orExit(30000);
if (listFiles.HasE xited)
{
// Read and display lines from the file until the end of
// the file is reached.
accountFile = fileOutput.Read ToEnd();
output = myOutput.ReadTo End();
if (accountFile != null && accountFile != "")
numOfAccounts =
CPAUnix.ParseAn dInsertAccounts (accountFile,
ref dtAccounts, ref slMain, ref ssMain, userOrGroup);
}
else
MessageBox.Show ("Time out retrieving NIS" + userOrGroup +
"accounts." ,
"PowerADvantage ");
}
else
MessageBox.Show ("Error starting process to read file.",
"PowerADvantage ");

return numOfAccounts;

}
catch (Win32Exception wex)
{
MessageBox.Show (wex.Message, "PowerADvantage ");
return numOfAccounts;
}
}
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
Pucca <Pu***@discussi ons.microsoft.c omwrote:
Hi, I'm using VS 2005, ,.net 2 for C# windows application. I'm using Process
to run a C application and redirecting its standard output so I can read it
with StreamReader.Re adToEnd. It's only reading the 1st 5 lines or so when
there is about 30 lines of data.
I also tried using the Process.BeginOu tputReadlin and a
DataReceivedEvn tHandler to gather output but I also only get about 5 lines of
the data.
There seems to be a buffer size issue. Can someone tell me how can I
resolve this problem. Thank you.

It should work fine. Do you know if your C application is actually
finishing? You should read from standard error as well as standard out
- otherwise if it's trying to write to standard error, it may be
blocking.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 3 '07 #3
Here is my code using BeginOutputRead Line. This code doesn't have the time
out issue but it only returns 5 lines of data.

static System.Text.Str ingBuilder sb = new System.Text.Str ingBuilder();
static void proc_OutputData Received(object sender, DataReceivedEve ntArgs e)
{
sb.Append(e.Dat a);
}

public static int GetNisFile(Syst em.Diagnostics. ProcessStartInf o psi,
ref DataTable dtAccounts,
ref ToolStripStatus Label slMain, ref StatusStrip ssMain, string
userOrGroup)
{

string accountFile = "", output = "", line = "", theResult="";
int numOfAccounts = 0;
System.IO.Strea mReader fileOutput = null, myOutput = null;
System.Diagnost ics.Process listFiles = new Process();
listFiles.Start Info = psi;
try
{
String inputText;
int numInputLines = 0;
listFiles.Outpu tDataReceived += new
DataReceivedEve ntHandler(proc_ OutputDataRecei ved);
//listFiles = System.Diagnost ics.Process.Sta rt(psi);
// Use a stream writer to synchronously write the sort input.
listFiles.Start ();
//StreamReader nisReader = listFiles.Stand ardOutput;
listFiles.Begin OutputReadLine( );
listFiles.WaitF orExit();
theResult = sb.ToString();
listFiles.Cance lOutputRead();
listFiles.Kill( );
return numOfAccounts;

}
catch (Win32Exception wex)
{
MessageBox.Show (wex.Message, "PowerADvantage ");
return numOfAccounts;
}
}
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
Pucca <Pu***@discussi ons.microsoft.c omwrote:
Hi, I'm using VS 2005, ,.net 2 for C# windows application. I'm using Process
to run a C application and redirecting its standard output so I can read it
with StreamReader.Re adToEnd. It's only reading the 1st 5 lines or so when
there is about 30 lines of data.
I also tried using the Process.BeginOu tputReadlin and a
DataReceivedEvn tHandler to gather output but I also only get about 5 lines of
the data.
There seems to be a buffer size issue. Can someone tell me how can I
resolve this problem. Thank you.

It should work fine. Do you know if your C application is actually
finishing? You should read from standard error as well as standard out
- otherwise if it's trying to write to standard error, it may be
blocking.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 3 '07 #4
Pucca <Pu***@discussi ons.microsoft.c omwrote:
Here is my code on first try to read the standard output. In the debug mode
I can see some lines of the data are read in but the process never has a
exited status. I even took away the time out value and it just hangs and
sits there and I can see the stream buffer hits a limit and hangs.
listFiles.WaitF orExit();
Is there a way to read all my data in without a buffer limitation? Thank you.
You need to read the data in a different thread, basically. I don't
have the time to write the code right now, but if you have one thread
reading the stdout and one reading stderr, you shouldn't see this
problem.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 5 '07 #5
Thanks Jon, I did read the article on the parent, child process deadlock
issue. I modified my code to use sync. read for the stderr and async. read
for the stadout. But, I'm still getting just a few lines of my stdout. I
ran the C application again and it gets over 100 lines of data. Thank you.
static System.Text.Str ingBuilder sb = new System.Text.Str ingBuilder();
static void proc_OutputData Received(object sender, DataReceivedEve ntArgs e)
{
sb.Append(e.Dat a);
}

public static int GetNisFile(Syst em.Diagnostics. ProcessStartInf o psi, string
parm,
ref DataTable dtAccounts,
ref ToolStripStatus Label slMain, ref StatusStrip ssMain, string
userOrGroup)
{

string accountFile = "", output = "", line = "", theResult="";
int numOfAccounts = 0;
String inputText;
int numInputLines = 0;
System.IO.Strea mReader fileOutput = null, myOutput = null;
System.Diagnost ics.Process listFiles = new Process();
//listFiles.Start Info = psi;
try
{
listFiles.Outpu tDataReceived += new
DataReceivedEve ntHandler(proc_ OutputDataRecei ved);
listFiles.Start Info = new ProcessStartInf o(Application.S tartupPath +
"\\ypcat.ex e", parm);
listFiles.Start Info.UseShellEx ecute = false;
listFiles.Start Info.RedirectSt andardOutput = true;
listFiles.Start Info.RedirectSt andardError = true;
//listFiles = System.Diagnost ics.Process.Sta rt(psi);
// Use a stream writer to synchronously write the sort input.
if (listFiles.Star t())
{
//StreamReader nisReader = listFiles.Stand ardOutput;
listFiles.Begin OutputReadLine( );
string error = listFiles.Stand ardError.ReadTo End();
listFiles.WaitF orExit();
if(listFiles.Ha sExited)
theResult = sb.ToString();
listFiles.Cance lOutputRead();
listFiles.Close ();
}
return numOfAccounts;

}
catch (Win32Exception wex)
{
MessageBox.Show (wex.Message, "PowerADvantage ");
return numOfAccounts;
}
}
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
Pucca <Pu***@discussi ons.microsoft.c omwrote:
Here is my code on first try to read the standard output. In the debug mode
I can see some lines of the data are read in but the process never has a
exited status. I even took away the time out value and it just hangs and
sits there and I can see the stream buffer hits a limit and hangs.
listFiles.WaitF orExit();
Is there a way to read all my data in without a buffer limitation? Thank you.

You need to read the data in a different thread, basically. I don't
have the time to write the code right now, but if you have one thread
reading the stdout and one reading stderr, you shouldn't see this
problem.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 6 '07 #6
On Aug 6, 8:58 am, Pucca <Pu...@discussi ons.microsoft.c omwrote:
Thanks Jon, I did read the article on the parent, child process deadlock
issue. I modified my code to use sync. read for the stderr and async. read
for the stadout. But, I'm still getting just a few lines of my stdout.
Without full code to run ourselves, it's pretty tricky to diagnose
this.

I suggest you first swap out the original process for a "dummy"
process written in C# which just writes 100 lines of output to both
stdout and stderror - then come up with a short but complete program
in C# which shows the problem occurring. That way we can all look at
the same issue.

Jon

Aug 6 '07 #7
Ok, I will do that. Sounds like a good test. Also, I forgot to mention that
I'm running on Windows 2000 server. Would this be the problem?
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
On Aug 6, 8:58 am, Pucca <Pu...@discussi ons.microsoft.c omwrote:
Thanks Jon, I did read the article on the parent, child process deadlock
issue. I modified my code to use sync. read for the stderr and async. read
for the stadout. But, I'm still getting just a few lines of my stdout.

Without full code to run ourselves, it's pretty tricky to diagnose
this.

I suggest you first swap out the original process for a "dummy"
process written in C# which just writes 100 lines of output to both
stdout and stderror - then come up with a short but complete program
in C# which shows the problem occurring. That way we can all look at
the same issue.

Jon

Aug 6 '07 #8
Pucca <Pu***@discussi ons.microsoft.c omwrote:
Ok, I will do that. Sounds like a good test. Also, I forgot to mention that
I'm running on Windows 2000 server. Would this be the problem?
Shouldn't be, no.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 6 '07 #9
I created 2 applications as you said and it works! So, I use the new C#
application and instead of calling the dummy program I called my C
application and display the output to a textbox and that works too! Now, I'm
really confused about why it doesn't work in my program.
--
Thanks.
"Jon Skeet [C# MVP]" wrote:
On Aug 6, 8:58 am, Pucca <Pu...@discussi ons.microsoft.c omwrote:
Thanks Jon, I did read the article on the parent, child process deadlock
issue. I modified my code to use sync. read for the stderr and async. read
for the stadout. But, I'm still getting just a few lines of my stdout.

Without full code to run ourselves, it's pretty tricky to diagnose
this.

I suggest you first swap out the original process for a "dummy"
process written in C# which just writes 100 lines of output to both
stdout and stderror - then come up with a short but complete program
in C# which shows the problem occurring. That way we can all look at
the same issue.

Jon

Aug 6 '07 #10

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

Similar topics

8
6820
by: codefixer | last post by:
Hi, I was wondering what factors will influence the maximum buffer sizes for source and destination for memcpy(). Is their any limit to the sizeof the buffer(hardware restricting it ?) OR any other limitations like the compiler being used ? Your thoughts are appreciated. Thanks.
6
7245
by: BMax | last post by:
Hello I want to create a byte and use it as a buffer, then access it with a binaryreader or streamreader. what is important is to be able to read different variables form that, like readint16, readbytes(),readint32.... . so if there is a method to read byte arrays that would also help. (i couldn' use convert class ) is that possible? if yes how?
4
1427
by: ad | last post by:
Have there any limitation about the size of the file uploaded by FileUpload control?
4
4620
by: chenatcr | last post by:
Hello, I added a serial-USB converter to my laptop, it appear as USB serial port COM4, and I wrote an application to read 78k data from this port. In my VC++ express code, I defined ReadBufferSize = 78k, and ReceivedBytesThreashold = 78k so that I can get the whole data with one ReadExisting() method when DataReceived event fires.
4
3959
by: aki | last post by:
Hi all, i am writing codes for implementing network protocols. i have written a method which is receiving a packet from network. i have assumed that the packet i will receive will be of type char*. i need to test my method and for that i want to create a network buffer which will contain a packet of format as-> example of Configuration req packet uint8_t code;
2
6614
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have to create a buffer of a pre-defined length: (ConstBufferByteSize=10000000). How can I read a stream into a buffer or string without knowing the number of chars the stream will contain?
0
3020
by: =?Utf-8?B?UHVjY2E=?= | last post by:
-- Hi I'm using vs2005, .net 2 for windows application. The application I started using System.Diagnostics.Process is having a "listFiles.StandardOutput" buffer size problem. I was wondering where and how can I adjust the buffer size problem. I don't want to be limited to a set buffer size, is that possible? thank you. public static int GetNisFile(System.Diagnostics.ProcessStartInfo psi, ref DataTable dtAccounts, ref...
1
3089
by: lapucca | last post by:
Hi, I'm using VS 2005, ,.net 2 for C# windows application. I'm using Process to run a C application and redirecting its standard output so I can read it with StreamReader.ReadToEnd. It's only reading the 1st 5 lines or so when there is about 100 lines of data. I also tried using the Process.BeginOutputReadlin and a DataReceivedEvntHandler to gather output but I also only get about 5 lines of the data. There seems to be a buffer...
0
916
by: bennett | last post by:
I have written the following function Public Function ReadFile(ByVal FileName As String) As String Dim objReader As StreamReader = Nothing Dim objData As New StringBuilder Dim strReturn As String = "" Try objReader = New StreamReader(FileName, Encoding.Default, True, 512) objData.Append(objReader.ReadToEnd) strReturn =...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10039
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9860
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6668
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5297
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.