如何使用StreamReader.ReadTo克服缓冲区限制 [英] How can I overcome the buffer limitation using StreamReader.ReadTo

查看:75
本文介绍了如何使用StreamReader.ReadTo克服缓冲区限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2005,.net 2进行C#windows应用程序。我正在使用Process

来运行C应用程序并重定向其标准输出,以便我可以使用StreamReader.ReadToEnd读取它/ b $ b。

只有大约30行数据才能读取前5行左右。

我也尝试使用Process.BeginOutputReadlin和

DataReceivedEvntHandler收集输出但我也只获得了大约5行

的数据。

似乎存在缓冲区大小问题。有人可以告诉我,我怎么能解决这个问题
。谢谢。

-

谢谢。

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 30 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 size issue. Can someone tell me how can I
resolve this problem. Thank you.
--
Thanks.

推荐答案

Pucca< Pu ** *@discussions.microsoft.comwrote:
Pucca <Pu***@discussions.microsoft.comwrote:

我正在使用VS 2005,.net 2用于C#windows应用程序。我正在使用Process

来运行C应用程序并重定向其标准输出,以便我可以使用StreamReader.ReadToEnd读取它/ b $ b。

只有大约30行数据才能读取前5行左右。

我也尝试使用Process.BeginOutputReadlin和

DataReceivedEvntHandler收集输出但我也只获得了大约5行

的数据。

似乎存在缓冲区大小问题。有人可以告诉我,我怎么能解决这个问题
。谢谢。
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 30 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 size issue. Can someone tell me how can I
resolve this problem. Thank you.



应该可以正常工作。你知道你的C应用程序是否实际上是b
整理?您应该阅读标准错误以及标准输出

- 否则如果它试图写入标准错误,则可能是

阻塞。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件

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.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这是我的代码,首先尝试读取标准输出。在调试模式下

我可以看到读入了一些数据行但是进程从未有过退出状态。我甚至拿走了超时值并且它只是挂起并且

坐在那里我可以看到流缓冲区达到限制并挂起。

listFiles.WaitForExit();

有没有办法在没有缓冲限制的情况下读取我的所有数据?谢谢。


public static int GetNisFile(System.Diagnostics.ProcessStartInfo psi,

ref DataTable dtAccounts,

ref ToolStripStatusLabel slMain ,ref StatusStrip ssMain,string

userOrGroup)

{


string accountFile ="",output ="" ;;

int numOfAccounts = 0;

System.IO.StreamReader fileOutput = null,myOutput = null;

System.Diagnostics.Process listFiles ;

试试

{

listFiles = System.Diagnostics.Process.Start(psi);

if(b) listFiles!= null)

{

myOutput = listFiles.StandardError;

fileOutput = listFiles.StandardOutput;

listFiles.WaitForExit(30000);

if(listFiles.HasExited)

{

//读取并显示文件中的行直到结束

//文件已到达。

accountFile = fileOutput.ReadToEnd();

output = myOutput.ReadToEnd();

if(accountFile!= null&& accountFile!="")

numOfAccounts =

CPAUnix.ParseAndInsertAccounts(accountFile,

ref dtAccounts,ref slMain,ref ssMain,userOrGroup );

}

其他

MessageBox.Show("超时检索NIS" + userOrGroup +

" accounts。",

" PowerADvantage");

}

else

MessageBox.Show (启动进程读取文件时出错。,

PowerADvantage);


返回numOfAccounts;


}

catch(Win32Exception wex)

{

MessageBox.Show(wex.Message," PowerADvantage");

返回numOfAccounts;

}

}

-

谢谢。

" Jon Skeet [C#MVP]"写道:
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.WaitForExit();
Is there a way to read all my data in without a buffer limitation? Thank you.

public static int GetNisFile(System.Diagnostics.ProcessStartInfo psi,
ref DataTable dtAccounts,
ref ToolStripStatusLabel slMain, ref StatusStrip ssMain, string
userOrGroup)
{

string accountFile="", output="";
int numOfAccounts = 0;
System.IO.StreamReader fileOutput = null, myOutput = null;
System.Diagnostics.Process listFiles;
try
{
listFiles = System.Diagnostics.Process.Start(psi);
if (listFiles != null)
{
myOutput = listFiles.StandardError;
fileOutput = listFiles.StandardOutput;
listFiles.WaitForExit(30000);
if (listFiles.HasExited)
{
// Read and display lines from the file until the end of
// the file is reached.
accountFile = fileOutput.ReadToEnd();
output = myOutput.ReadToEnd();
if (accountFile != null && accountFile != "")
numOfAccounts =
CPAUnix.ParseAndInsertAccounts(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 *** @ discussion.microsoft.comwrote:
Pucca <Pu***@discussions.microsoft.comwrote:

我正在使用VS 2005,.net 2进行C#windows应用程序。我正在使用Process

来运行C应用程序并重定向其标准输出,以便我可以使用StreamReader.ReadToEnd读取它/ b $ b。

只有大约30行数据才能读取前5行左右。

我也尝试使用Process.BeginOutputReadlin和

DataReceivedEvntHandler收集输出但我也只获得了大约5行

的数据。

似乎存在缓冲区大小问题。有人可以告诉我,我怎么能解决这个问题
。谢谢。
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 30 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 size issue. Can someone tell me how can I
resolve this problem. Thank you.



它应该可以正常工作。你知道你的C应用程序是否实际上是b
整理?您应该阅读标准错误以及标准输出

- 否则如果它试图写入标准错误,则可能是

阻塞。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件


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.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这是我使用BeginOutputReadLine的代码。这段代码没有时间

out问题,但它只返回5行数据。


static System.Text.StringBuilder sb = new System .Text.StringBuilder();

static void proc_OutputDataReceived(object sender,DataReceivedEventArgs e)

{

sb.Append(e.Data) ;

}


public static int GetNisFile(System.Diagnostics.ProcessStartInfo psi,

ref DataTable dtAccounts,

ref ToolStripStatusLabel slMain,ref StatusStrip ssMain,string

userOrGroup)

{


string accountFile ="" ;,output ="",line ="",theResult ="" ;;

int numOfAccounts = 0;

System.IO.StreamReader fileOutput = null,myOutput = null;

System.Diagnostics.Process listFiles = new Process();

listFiles.StartInfo = psi;

try

{

String inputText;

int numInputLines = 0;

listFi les.OutputDataReceived + = new

DataReceivedEventHandler(proc_OutputDataReceived);

// listFiles = System.Diagnostics.Process.Start(psi);

//使用流编写器同步写入排序输入。

listFiles.Start();

// StreamReader nisReader = listFiles.StandardOutput;

listFiles.BeginOutputReadLine();

listFiles.WaitForExit();

theResult = sb.ToString();

listFiles.CancelOutputRead( );

listFiles.Kill();

返回numOfAccounts;


}

catch( Win32Exception wex)

{

MessageBox.Show(wex.Message," PowerADvantage");

返回numOfAccounts;

}

}

-

谢谢。

" Jon Skeet [C#MVP]" ;写道:
Here is my code using BeginOutputReadLine. This code doesn''t have the time
out issue but it only returns 5 lines of data.

static System.Text.StringBuilder sb = new System.Text.StringBuilder();
static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
sb.Append(e.Data);
}

public static int GetNisFile(System.Diagnostics.ProcessStartInfo psi,
ref DataTable dtAccounts,
ref ToolStripStatusLabel slMain, ref StatusStrip ssMain, string
userOrGroup)
{

string accountFile = "", output = "", line = "", theResult="";
int numOfAccounts = 0;
System.IO.StreamReader fileOutput = null, myOutput = null;
System.Diagnostics.Process listFiles = new Process();
listFiles.StartInfo = psi;
try
{
String inputText;
int numInputLines = 0;
listFiles.OutputDataReceived += new
DataReceivedEventHandler(proc_OutputDataReceived);
//listFiles = System.Diagnostics.Process.Start(psi);
// Use a stream writer to synchronously write the sort input.
listFiles.Start();
//StreamReader nisReader = listFiles.StandardOutput;
listFiles.BeginOutputReadLine();
listFiles.WaitForExit();
theResult = sb.ToString();
listFiles.CancelOutputRead();
listFiles.Kill();
return numOfAccounts;

}
catch (Win32Exception wex)
{
MessageBox.Show(wex.Message, "PowerADvantage");
return numOfAccounts;
}
}
--
Thanks.
"Jon Skeet [C# MVP]" wrote:

Pucca< Pu *** @ discussion.microsoft.comwrote:
Pucca <Pu***@discussions.microsoft.comwrote:

我正在使用VS 2005,.net 2进行C#windows应用程序。我正在使用Process

来运行C应用程序并重定向其标准输出,以便我可以使用StreamReader.ReadToEnd读取它/ b $ b。

只有大约30行数据才能读取前5行左右。

我也尝试使用Process.BeginOutputReadlin和

DataReceivedEvntHandler收集输出但我也只获得了大约5行

的数据。

似乎存在缓冲区大小问题。有人可以告诉我,我怎么能解决这个问题
。谢谢。
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 30 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 size issue. Can someone tell me how can I
resolve this problem. Thank you.



它应该可以正常工作。你知道你的C应用程序是否实际上是b
整理?您应该阅读标准错误以及标准输出

- 否则如果它试图写入标准错误,则可能是

阻塞。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件


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.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于如何使用StreamReader.ReadTo克服缓冲区限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆