Execute all .bat files in the current folder and delete them after execution

I'm a novice programmer looking to do video averaging. I made a program to create n.bat files executing an average of n images, now I would like to execute them as quickly as possible.

Bat files are independent. I am in a Windows environment.

I've looked at C # multithreading (threadpool, parrallel.for, parralel.foreach, etc.), but none of the functions work there. I have no illusion that it is me who is doing something wrong, though.

Powershell has a function that does what I want, but only for other powershell commands.

The code I have now basically works: (complete solution at https://github.com/Madsfoto/ParallelExecutionForEach )

var paths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.bat"); // have a list of all .bat files in the current directory
System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.UseShellExecute = false; // above is to not see the cmd window

            proc.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory(); // It easier than having to specify where this program will be run.



Parallel.ForEach(paths, new ParallelOptions { MaxDegreeOfParallelism = 4 }, currentFile => // 4 is set because I have 4 cores to use
                {
                    proc.StartInfo.FileName = currentFile; // Set the currentfile as the one being executed. currentFile is the name of the .bat file to execute
    proc.Start(); // execute the .bat file
                        proc.WaitForExit();
                    File.Delete(currentFile);
                });

      

I get System.InvalidOperationException: No process is associated with this object

and System.UnauthorizedAccessException

s when I run more than 3-4 processes at the same time.

I suspect it is WaitForExit () that is causing me problems, but I have no skills to debug it.

I also looked at Threading.Task but my skill is not enough to use it.

So, the solution I got is the following:

Execute either 1 input file with x independent action lines, or x 1 action files, limiting y processes at the same time, either at compile time or at runtime.
The programming language is not important to me, although I prefer to understand C #.

(The result looks like https://www.youtube.com/watch?v=ph6-6bYTgs0 when n frames are averaged together)

+3


source to share


1 answer


The solution was to move

proc.Start(); // execute the .bat file
proc.WaitForExit();
try
{
   File.Delete(FileName);
}
catch
{
}

      

code into its own function (with accounting (definition of proc, etc.)).



File.Delete () was the culprit, it turned out that there might be a bug in Parallel.ForEach, but I couldn't reliably reproduce it (experiment gives errors ~ 0.01% of the time), but that's how it works. This requires people to re-launch the executable, but it's a burden I can justify by clicking on the user.

The github link has been updated with a working version.

0


source







All Articles