如何重用具有稍微不同的ProcessStartInfo实例的Process实例? [英] How to reuse a Process instance with slightly different ProcessStartInfo instances?

查看:128
本文介绍了如何重用具有稍微不同的ProcessStartInfo实例的Process实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码启动 robocopy 作为进程。我还需要做数据库查询来确定每次调用哪些目录需要复制 robocopy 被调用,所以我使用 ProcessStartInfo 控制参数传递。

I have the following code that starts robocopy as a Process. I also need to do database queries to determine which directories I need to copy each time robocopy is called so I used ProcessStartInfo to control the arguments passed.

internal class Program
{
    private static void Main(string[] args)
    {
        using (var context = new MyDbContext())
        {
            IEnumerable<ProcessStartInfo> processInfos = GetProcessInfos(context, args[0]);
            foreach (ProcessStartInfo processInfo in processInfos)
            {
                // How can I reuse robocopy Process instances and
                // how can I dispose of them properly?
                Process.Start(processInfo);
            }
        }
    }

    private static IEnumerable<ProcessStartInfo> GetProcessInfos(MyDbContext context,
                                                                 string directory)
    {
        const string defaultRobocopyFormatString = "{0} {1} /mir /tee /fft /r:3 /w:10 /xd *Temp*";
        var directoryInfo = new DirectoryInfo(directory);
        return from dir in directoryInfo.GetDirectories()
               from myEntity in context.MyEntities
               where dir.Name == myEntity.Name
               select new ProcessStartInfo("robocopy", 
                                           string.Format(defaultRobocopyFormatString,
                                                         Path.Combine("C:\Test", dir.Name), 
                                                         Path.Combine("C:\Test_bak", dir.Name)));
    }
}

如何重用 foreach 循环,如何正确地处理?

How can I reuse Process instances returned by the static Process.Start(ProcessStartInfo) inside the foreach loop and how can I Dispose of them properly?

推荐答案

不能重新使用Process对象。 Process类的行为与包装操作系统对象的所有其他.NET类一样。像Socket,Bitmap,Mutex,FileStream等。它们是微小的饼干,非常便宜,可以在GC堆上烘烤并占用很少的空间。他们会仔细跟踪底层操作系统对象的生命周期,一旦对象死机,.NET包装器对象也不再有用。

You cannot re-use a Process object. The Process class behaves like all of the other .NET classes that wrap an operating system object. Like Socket, Bitmap, Mutex, FileStream, etcetera. They are tiny little cookies that are very cheap to bake and take very little space on the GC heap. They track the lifetime of the underlying OS object carefully, once the object is dead, the .NET wrapper object is no longer useful either.

Process类表示cookie被其Exited事件和HasExited属性吃掉。它有一些后期属性,有用的ExitCode和ExitTime。

The Process class signals that the cookie was eaten with its Exited event and HasExited property. It has some post-bite properties that are useful, ExitCode and ExitTime.

但是这是它的结束,如果你想创建另一个进程,那么你必须烘烤另一个cookie 。使用新的关键字或Start()工厂函数很简单。不要试图优化它,没有意义,它不能工作。重新使用ProcessStartInfo很好,它不是一个包装类。

But that's where it ends, if you want to create another process then you have to bake another cookie. Simple to do with the new keyword or the Start() factory function. Don't try to optimize it, there's no point and it can't work. Re-using ProcessStartInfo is fine, it is not a wrapper class.

这篇关于如何重用具有稍微不同的ProcessStartInfo实例的Process实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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