如何将数据从多个过程重定向到多种形式 [英] How to redirect data from multiple process to multiple form

查看:52
本文介绍了如何将数据从多个过程重定向到多种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台程序,该程序处理单个文件,但没有源代码.我想使用此程序同时处理多个文件,但一次不超过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?

推荐答案

确定,创建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.


您可以使用线程池轻松限制在任何给定时间运行的线程数.本文可能使您感兴趣:

多线程,委托和自定义事件 [
You can easily limit the number of threads running at any given time by using a thread pool. This article might interest you:

Multithreading, Delegates, and Custom Events[^]


这篇关于如何将数据从多个过程重定向到多种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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