How to reuse a process instance with multiple different ProcessStartInfo instances?

I have the following code that runs robocopy

like Process

. I also need to do database queries to determine which directories I need to copy each time it is called robocopy

, so I used ProcessStartInfo

to manage the passed arguments.

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)));
    }
}

      

How can I reuse the instances Process

returned static

Process.Start(ProcessStartInfo)

inside the loop foreach

, and how can I get them right Dispose

?

+3


source to share


2 answers


You cannot reuse the Process object. The Process class behaves like all other .NET classes that wrap an operating system object. Like Socket, Bitmap, Mutex, FileStream, etc. These are tiny pieces that are very cheap to bake and take up very little space on the GC heap. They keep track of the lifetime of the underlying OS object carefully, once the object is dead, the .NET wrapper object is also not useful.

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



But when it ends, if you want to create another process, you need to bake another cookie. Simple use of a new keyword or Start () factory function. Don't try to optimize it, it makes no sense and it can't work. Reusing ProcessStartInfo is ok, it is not a wrapper class.

+7


source


You don't need to reuse the class Process

- it's just a wrapper around the main process. And when the processes end, they completely disappeared - the main thing is to have a process in the first place.

Instead, it seems like you really want to just make sure only one of these processes is robocopy

running at the same time, which is pretty straightforward:



using (var context = new MyDbContext())
{
    IEnumerable<ProcessStartInfo> processInfos = GetProcessInfos(context, args[0]);
    foreach (ProcessStartInfo processInfo in processInfos)
    {
        using (var process = Process.Start(processInfo))
        {
            // Blocks until the process ends
            process.WaitForExit();
        }
        // When the `using` block is left, `process.Dispose()` is called.
    }
}

      

+1


source







All Articles