数据从多个过程重定向到多种形式
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)
.为流设置属性StandardOutput
和StandardError
(以防程序在此流中输出一些错误信息).在作为参数传递的类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 classSystem.Diagnostics.Process
and its methodSystem.Diagnostics.Process.Start(ProcessStartInfo)
. Set the propertiesStandardOutput
andStandardError
(just in case your program output some error information in this stream) to your streams. In the instance of the classSystem.Diagnostics.ProcessStartInfo
passed as a parameter assignStartInfo.RedirectStandardOutput
totrue
.
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
系列文章
更多
同类精品
更多
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20