• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

数据从多个过程重定向到多种形式

用户头像
it1352
帮助1

问题说明

我有一个控制台程序,该程序处理单个文件,但没有源代码.我想使用此程序同时处理多个文件,但一次不超过8个文件.我还想将标准输出重定向到Windows窗体而不是Shell窗口.是否可以使用C#?

感谢您的回复.

Hi, I have a console program which process a single file and do not have the source code of it. I would like to process multiple files with this program simultaneously but not more than 8 file at a time. I would also like to re-direct the standard output to windows form instead of the shell window. Is it possible using C#?

Thanks for your reply.

Anyone has answer to the question?

正确答案

#1
确定,创建8个线程(为什么8个线程?我认为这没有道理),在每个线程中启动一个进程与您的控制台程序一起使用,并将其控制台输出重定向到您的流.您应该没有任何问题.

使用类System.Diagnostics.Process及其方法System.Diagnostics.Process.Start(ProcessStartInfo).为流设置属性StandardOutputStandardError(以防程序在此流中输出一些错误信息).在作为参数传递的类System.Diagnostics.ProcessStartInfo的实例中,将StartInfo.RedirectStandardOutput分配给true.

这是一个代码示例:
http://msdn.microsoft.com/en-us/library/system. diagnostics.process.standardoutput.aspx [ ^ ].

另请参见:
http://msdn.microsoft.com/en-us/library/system.diagnostics. process.aspx [^ ],
http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.diagnostics. processstartinfo.aspx [ ^ ].

—SA
Sure, create 8 threads (why 8? I don''t think it makes sense), in each thread start a process with your console program and redirect its console output to your stream. You should not have any problems.

Use the class System.Diagnostics.Process and its method System.Diagnostics.Process.Start(ProcessStartInfo). Set the properties StandardOutput and StandardError (just in case your program output some error information in this stream) to your streams. In the instance of the class System.Diagnostics.ProcessStartInfo passed as a parameter assign StartInfo.RedirectStandardOutput to true.

Here is a code sample:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

See also:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^],
http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx[^].

—SA
您需要了解的一件事.如果您正在使用文件,则您的8个线程可能会比一个慢.为什么 ?因为他们将争夺对一个硬盘的访问权限.如果您正在执行内存中的所有操作,那么使用8个线程仍然不会使您的计算机更快.如果您有很多核心,它可能会利用更多的核心,从而加快了处理速度,但是很可能除非您拥有8个核心(我有可能,否则),否则可能会有8个线程构成您的代码更加复杂,而不会在不像您希望的那么快的地方加快结果的速度.
One thing you need to know. If you''re working with files, your 8 threads will probably be slower than one. Why ? Because they will be competing for access to the one hard drive. If you''re doing everything in memory, using 8 threads still won''t make your computer faster. If you have many cores, it may utilise more cores, and thus speed up the process, but the odds are good that unless you have 8 cores ( I do, so it''s possible ), 8 threads are likely to make your code more complex, without speeding up the result anywhere near as much as you''re hoping.
您可以使用线程池轻松限制在任何给定时间运行的线程数.本文可能使您感兴趣:

多线程,委托和自定义事件 [查看全文

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhbjkeeb
系列文章
更多 icon
同类精品
更多 icon
继续加载