繁体   English   中英

c#中的System.Diagnostics.Process和c ++中的CreateProcess()是最佳选择?

[英]System.Diagnostics.Process in c# and CreateProcess() in c++ which one is the best choice?

当我想在我的application(c#)中启动一个子流程时,在c#中的System.Diagnostics.Process和在c ++中的CreateProcess()中,哪个是更好的选择?

将参数提供给subProcess隐藏子流程的窗口Gead子流程的输出,例如日志等。

另外,我想设置CPU数量并按用户限制subProcess的内存。

我想安全地杀死所有子进程(而不是泄漏内存和一些子进程),以便用户停止父进程

System.Diagnostics.Process会很好。 始终从托管代码调用托管代码方法。

我猜System.Diagnostics.Process将是一个不错的选择,因为您的应用程序得到了管理。 如果您不想隐藏子流程,请不要设置:

the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.UseShellExecute = false;

要限制内核数量,您必须尝试使用ProcessThread.ProcessorAffinity

using System;
using System.Diagnostics;

namespace ProcessThreadIdealProcessor
{
    class Program
    {
        static void Main(string[] args)
        {
            // Make sure there is an instance of notepad running.
            Process[] notepads = Process.GetProcessesByName("notepad");
            if (notepads.Length == 0)
                Process.Start("notepad");
            ProcessThreadCollection threads;
            //Process[] notepads; 
            // Retrieve the Notepad processes.
            notepads = Process.GetProcessesByName("Notepad");
            // Get the ProcessThread collection for the first instance
            threads = notepads[0].Threads;
            // Set the properties on the first ProcessThread in the collection
            threads[0].IdealProcessor = 0;
            threads[0].ProcessorAffinity = (IntPtr)1;
        }
    }
}

要限制内存使用量集Process.MaxWorkingSet它可以帮助您最大程度地提高关联进程的允许工作集大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM