重定向StandardOutput [英] Redirecting StandardOutput

查看:87
本文介绍了重定向StandardOutput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了重定向我在应用程序中运行DOS程序的过程的StandardOutput的问题。问题是,当我开始我的进程时(我在一个单独的线程中执行)我无法在进程实际完成之前从stdout读取任何内容

。如果我做了类似

的事情:


处理myProcess = new进程(myStartInfo)

//这会立即显示

MessageBox.Show(" Process Started");

string s = myProcess.StandardOutput.Readline();

//这不是在流程完成之前不会显示

MessageBox.Show(s);

myProcess.WaitForExit();

// this在最后一个MessageBox之后立即显示

MessageBox.Show(Process Finished);


很明显我不是在读在进程之前stdout的任何内容

已经完成。这是一个问题,因为我运行的程序将

文件传输到服务器并且可能需要很长时间才能完成,但是我无法给用户提供

有关转移过程中发生的事情的任何信息。

有人可以告诉我如何阅读标准输出而不挂东西

直到过程结束?我不敢相信没有办法这样做

这个!!


干杯

I''m having a problem with redirecting the StandardOutput of a process that I
use to run a DOS program in my application. The problem is that I when I
start my process (which I do in a separate thread) I can''t read anything
from the stdout before the process actually finishes. If I do something like
this:

Process myProcess = new Process(myStartInfo)
// this gets shown straight away
MessageBox.Show("Process Started");
string s = myProcess.StandardOutput.Readline();
// this doesn''t get shown until the process has finished
MessageBox.Show(s);
myProcess.WaitForExit();
// this gets shown immediately following the last MessageBox
MessageBox.Show("Process Finished");

It''s clear that I''m not reading anything from the stdout before the process
has finished. This is a problem because the program I am running transfers
files to a server and can take a long time to finish, but I''m unable to give
the user any information about what is going on during the transfer.
Can somebody please tell me how to read the stdout without hanging up things
up until the process has finished? I can''t believe there isn''t a way to do
this!!

Cheers

推荐答案

Matt Burland< wjousts @ [nospam] hotmail.com>写道:
Matt Burland <wjousts@[nospam]hotmail.com> wrote:
我遇到了重定向我在应用程序中运行DOS程序的进程的StandardOutput的问题。问题是,当我启动我的进程时(我在一个单独的线程中执行),在进程实际完成之前,我无法从stdout读取任何内容。如果我做了类似
这样的事情:

处理myProcess = new Process(myStartInfo)
//这会立即显示
MessageBox.Show(Process Started ;);
字符串s = myProcess.StandardOutput.Readline();
//这个过程完成后才会显示
MessageBox.Show(s);
myProcess.WaitForExit();
//这会立即显示在最后一个MessageBox之后
MessageBox.Show(Process Finished);

很明显在整个过程结束之前,我没有从标准输出中读到任何东西。这是一个问题,因为我运行的程序将文件传输到服务器并且可能需要很长时间才能完成,但我无法向用户提供有关在此期间发生的事情的任何信息。转移。
有人可以告诉我如何阅读标准品而不挂东西
直到过程结束?我不敢相信没有办法做这个!!
I''m having a problem with redirecting the StandardOutput of a process that I
use to run a DOS program in my application. The problem is that I when I
start my process (which I do in a separate thread) I can''t read anything
from the stdout before the process actually finishes. If I do something like
this:

Process myProcess = new Process(myStartInfo)
// this gets shown straight away
MessageBox.Show("Process Started");
string s = myProcess.StandardOutput.Readline();
// this doesn''t get shown until the process has finished
MessageBox.Show(s);
myProcess.WaitForExit();
// this gets shown immediately following the last MessageBox
MessageBox.Show("Process Finished");

It''s clear that I''m not reading anything from the stdout before the process
has finished. This is a problem because the program I am running transfers
files to a server and can take a long time to finish, but I''m unable to give
the user any information about what is going on during the transfer.
Can somebody please tell me how to read the stdout without hanging up things
up until the process has finished? I can''t believe there isn''t a way to do
this!!




我没有看到这个问题 - 是标准输出(不标准

错误)肯定写完整行文字?你能给一个简短的

,但完整的例子? (我记得写一个小的回声程序a / $
,前面显示标准输入和标准输出工作,对于

例子。)

-

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

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



I haven''t seen this problem - is the standard output (not standard
error) definitely writing whole lines of text? Could you give a short
but complete example? (I remember writing a small "echo" program a
while ago which showed standard input and standard output working, for
example.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Matt Burland< wjousts @ [nospam] hotmail.com>写道:
Matt Burland <wjousts@[nospam]hotmail.com> wrote:
我遇到了重定向我在应用程序中运行DOS程序的进程的StandardOutput的问题。问题是,当我启动我的进程时(我在一个单独的线程中执行),在进程实际完成之前,我无法从stdout读取任何内容。如果我做了类似
这样的事情:

处理myProcess = new Process(myStartInfo)
//这会立即显示
MessageBox.Show(Process Started ;);
字符串s = myProcess.StandardOutput.Readline();
//这个过程完成后才会显示
MessageBox.Show(s);
myProcess.WaitForExit();
//这会立即显示在最后一个MessageBox之后
MessageBox.Show(Process Finished);

很明显在整个过程结束之前,我没有从标准输出中读到任何东西。这是一个问题,因为我运行的程序将文件传输到服务器并且可能需要很长时间才能完成,但我无法向用户提供有关在此期间发生的事情的任何信息。转移。
有人可以告诉我如何阅读标准品而不挂东西
直到过程结束?我不敢相信没有办法做这个!!
I''m having a problem with redirecting the StandardOutput of a process that I
use to run a DOS program in my application. The problem is that I when I
start my process (which I do in a separate thread) I can''t read anything
from the stdout before the process actually finishes. If I do something like
this:

Process myProcess = new Process(myStartInfo)
// this gets shown straight away
MessageBox.Show("Process Started");
string s = myProcess.StandardOutput.Readline();
// this doesn''t get shown until the process has finished
MessageBox.Show(s);
myProcess.WaitForExit();
// this gets shown immediately following the last MessageBox
MessageBox.Show("Process Finished");

It''s clear that I''m not reading anything from the stdout before the process
has finished. This is a problem because the program I am running transfers
files to a server and can take a long time to finish, but I''m unable to give
the user any information about what is going on during the transfer.
Can somebody please tell me how to read the stdout without hanging up things
up until the process has finished? I can''t believe there isn''t a way to do
this!!




我没有看到这个问题 - 是标准输出(不标准

错误)肯定写完整行文字?你能给一个简短的

,但完整的例子? (我记得写一个小的回声程序a / $
,前面显示标准输入和标准输出工作,对于

例子。)

-

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

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



I haven''t seen this problem - is the standard output (not standard
error) definitely writing whole lines of text? Could you give a short
but complete example? (I remember writing a small "echo" program a
while ago which showed standard input and standard output working, for
example.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


好的,感谢您的回复,这是我写的一个简短的测试应用程序,显示

与我正在尝试的应用程序相同的行为写。我试图运行
的过程是pscp.exe,可以从这里下载:
http://www.chiark.greenend.org.uk/~s.../download.html

(注意:如果有人知道更好的方法来将scp写入unix盒子,那么我会对这里感兴趣的是b $ b。)

>
使用System;


使用System.Diagnostics;


使用System.Threading;


使用System.IO;


命名空间ProcessTest


{


class Class1


{


公共字符串m_ProcessArgs;


公共字符串m_ProcessApp;


public string m_Password;


public string m_UserID;


public string m_Address;


public string m_CalcPath;


public Process m_Running;


public StreamReader m_ StdOut;


///< summary>


///应用程序的主要入口点。


///< / summary>


[STAThread]


static void Main(string [] args)


{


Class1 myClass = new Class1();


myClass.m_UserID =" ; *******" ;;


myClass.m_Address =" ********" ;; //服务器地址

myClass.m_CalcPath =" / ********* /" ;; //服务器上的路径


myClass.m_Password =" ******" ;;


myClass.PutFiles(@" ; C:\ Data");


// *******出于安全原因删除!!


控制台。 ReadLine();


}


public void PutFiles(string FilePath)


{


m_ProcessArgs =" -r -pw" + m_Password +" " + FilePath +" " + m_UserID +

" @" + m_Address +":" + m_CalcPath;


m_ProcessApp =" pscp.exe" ;;


Console.WriteLine(" Starting transfer thread");


线程myThread =新线程(新的ThreadStart(TransferProcess));


myThread.Start();


Console.WriteLine(传输线程已经启动);


}


private void TransferProcess()


{


ProcessStartInfo pInfo = new ProcessStartInfo(m_ProcessApp,m_ProcessArgs);


pInfo.UseShellExecute = false;


pInfo.RedirectStandardOutput = true;


pInfo.RedirectStandardError = true;


pInfo.CreateNoWindow = true;


m_Running = new Process();


m_Running.StartInfo = pInfo;


Console.WriteLine(TransferProcess正在启动进程......);


m_Running.Start();


//此行显示为immedi ately


Console.WriteLine(The Process has started);


m_StdOut = m_Running.StandardOutput;


//在该过程完成之前,该行不会出现


Console.WriteLine(这是从StdOut中读取的: + m_StdOut.Read());


m_Running.WaitForExit();


//这出现在最后一行之后,表明最后的

行没有显示


//直到流程结束


控制台。 WriteLine(文件传输完成);


Console.ReadLine();


}


}


}

" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
Okay, thanks for your response, here is a short test app I wrote that shows
the same behavior as the app I''m trying to write. The process I''m trying to
run is pscp.exe which can be downloaded from here:
http://www.chiark.greenend.org.uk/~s.../download.html
(NB: If anybody knows a better way to do scp to a unix box, I''d be
interested to here about it.)

using System;

using System.Diagnostics;

using System.Threading;

using System.IO;

namespace ProcessTest

{

class Class1

{

public string m_ProcessArgs;

public string m_ProcessApp;

public string m_Password;

public string m_UserID;

public string m_Address;

public string m_CalcPath;

public Process m_Running;

public StreamReader m_StdOut;

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

Class1 myClass = new Class1();

myClass.m_UserID = "*******";

myClass.m_Address = "********"; // address of server

myClass.m_CalcPath = "/*********/"; // path on server

myClass.m_Password = "******";

myClass.PutFiles(@"C:\Data");

// ******* removed for security reasons!!

Console.ReadLine();

}

public void PutFiles(string FilePath)

{

m_ProcessArgs = "-r -pw " + m_Password + " " + FilePath + " " + m_UserID +
"@" + m_Address + ":" + m_CalcPath;

m_ProcessApp = "pscp.exe";

Console.WriteLine("Starting transfer thread");

Thread myThread = new Thread(new ThreadStart(TransferProcess));

myThread.Start();

Console.WriteLine("The transfer thread has been started");

}

private void TransferProcess()

{

ProcessStartInfo pInfo = new ProcessStartInfo(m_ProcessApp,m_ProcessArgs);

pInfo.UseShellExecute = false;

pInfo.RedirectStandardOutput = true;

pInfo.RedirectStandardError = true;

pInfo.CreateNoWindow = true;

m_Running = new Process();

m_Running.StartInfo = pInfo;

Console.WriteLine("TransferProcess is starting process....");

m_Running.Start();

// This line appears immediately

Console.WriteLine("The Process has started");

m_StdOut = m_Running.StandardOutput;

// This line doesn''t appear until the process has completed

Console.WriteLine("This was read from the StdOut: " + m_StdOut.Read());

m_Running.WaitForExit();

// This appears immediately after the last line, indicating that the last
line wasn''t displayed

// until the end of the process

Console.WriteLine("File Transfer Done");

Console.ReadLine();

}

}

}
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Matt Burland< wjousts @ [nospam] hotmail.com>写道:
Matt Burland <wjousts@[nospam]hotmail.com> wrote:
我遇到了重定向我用来在我的应用程序中运行DOS程序的进程
的StandardOutput的问题。问题是,当我启动我的进程时(我在一个单独的线程中执行),在进程实际完成之前,我无法从stdout读取任何内容。如果我像这样做一些


处理myProcess = new Process(myStartInfo)
//这会立即显示
MessageBox.Show(Process Started ;);
字符串s = myProcess.StandardOutput.Readline();
//这个过程完成后才会显示
MessageBox.Show(s);
myProcess.WaitForExit();
//这会立即显示在最后一个MessageBox之后
MessageBox.Show(Process Finished);

很明显在
流程完成之前,我没有从stdout读取任何内容。这是一个问题,因为我正在运行的程序
将文件传输到服务器并且可能需要很长时间才能完成,但是我无法向
提供有关在此期间发生的事情的任何信息转移。
有人可以告诉我如何阅读标准输出而不挂掉
的东西,直到过程结束?我不能相信没有办法
这样做!!
I''m having a problem with redirecting the StandardOutput of a process that I use to run a DOS program in my application. The problem is that I when I
start my process (which I do in a separate thread) I can''t read anything
from the stdout before the process actually finishes. If I do something like this:

Process myProcess = new Process(myStartInfo)
// this gets shown straight away
MessageBox.Show("Process Started");
string s = myProcess.StandardOutput.Readline();
// this doesn''t get shown until the process has finished
MessageBox.Show(s);
myProcess.WaitForExit();
// this gets shown immediately following the last MessageBox
MessageBox.Show("Process Finished");

It''s clear that I''m not reading anything from the stdout before the process has finished. This is a problem because the program I am running transfers files to a server and can take a long time to finish, but I''m unable to give the user any information about what is going on during the transfer.
Can somebody please tell me how to read the stdout without hanging up things up until the process has finished? I can''t believe there isn''t a way to do this!!



我没有看到这个问题 - 是标准输出(不是标准
错误)绝对写整行文字?你能举一个简短但完整的例子吗? (我记得在一段时间之前写了一个小的回声程序,显示标准输入和标准输出工作,例如

-
Jon Skeet - < sk *** @ pobox.com>
http:// www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



I haven''t seen this problem - is the standard output (not standard
error) definitely writing whole lines of text? Could you give a short
but complete example? (I remember writing a small "echo" program a
while ago which showed standard input and standard output working, for
example.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



这篇关于重定向StandardOutput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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