Home > database >  How to Call a third party program for each item in a list one by one in C#
How to Call a third party program for each item in a list one by one in C#

Time:11-12

I am trying to create a program that will unrar a set of files and then run a third party program on each of the unrar'd files one by one. I can get it to unrar the files correctly, and it will run the third party program (wxPirs.exe) on the first unrar'd file but then it stops. The code I have so far is below.

static void unpackRar()
{
    string homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
    string packedPath = homePath   "\\XBLA\\";
    string unpackedPath = homePath   "\\XBLA_Unpacked\\";
    string unrarPath = homePath   "\\unRAR.exe";

    {
        var process = Process.Start(unrarPath, " x "   packedPath   "*.rar  "   unpackedPath);
        process.WaitForExit();
    }
}

static List<string> GetAllFiles(string unpackedPath)
{
    return Directory.GetFiles(unpackedPath, "*", SearchOption.AllDirectories).ToList();
}

static void unPirs()
{
    string homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
    string unpackedPath = homePath   "\\XBLA_Unpacked\\";
    string wxPirsPath = homePath   "\\wxPirs.exe";
    for (int i = 0; i < GetAllFiles(unpackedPath).Count; i  )
    {
        Console.WriteLine(GetAllFiles(unpackedPath)[i]);
        var process =  Process.Start(wxPirsPath, GetAllFiles(unpackedPath));
        process.WaitForExit();
    }
}

I have tried using a for loop and Process.WaitForExit, but that did not seem to work as I expected it to.

CodePudding user response:

First of all, regarding the comment: they are right. You can GREATLY improve performance by changing that method to only call GetAllFiles() once, and especially not again for every loop iteration.

Second, regarding the issue in the question, I noticed the call to Process.Start() is missing the [i] after GetAllFiles(). Again, you want to use this on a variable from a single call to the method, rather than calling it again each time in the loop. To help avoid that kind of mistake in the future we can also use a foreach loop rather than a for loop.

Put it all together like this:

static void unPirs()
{
    string homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
    string unpackedPath = homePath   "\\XBLA_Unpacked\\";
    string wxPirsPath = homePath   "\\wxPirs.exe";
    var files = GetAllFiles(unpackedPath);

    for (var file in files)
    {
        Console.WriteLine(file);
        var process =  Process.Start(wxPirsPath, "\""   file "\"");
        process.WaitForExit();
    }
}

If it were me, this also seems like a good case for some asynchronous programming. I might queue up several Tasks to have going all at the same time (typically 2 * the number of CPU cores is a good starting place). Every time one finishes add another until they are all running. Then, wait for them all to finish.

  •  Tags:  
  • c#
  • Related