-1

I am running a .NET 4.7 C# console application in which I am iterating through a collection of files (list of strings with file paths.

I want to run an operation on each file in parallel.

private void LaunchComparators()
{
    //1) Get Trade Files
    var files = GetTradeFiles();

    //2) Run comparisons
    try
    {
        Parallel.ForEach(files, file => LaunchComparator(file));
    }
    catch (Exception ex)
    {
        Log.Error(ex.Message);
        throw ex;
    }
    //2 Write Results
    WriteResults();
}

private void LaunchComparator(string file)
{
    var comparator = new TradeComparator();
    var strategyComparisonOutput = comparator.ComparePerStrategy(file);

}

While running the comparison, the first comparison completes and then the program abruptly stops without any exceptions.

I am not sure what I should do differently here, so that the files are all processed individually.

I am new to parallel programming/threading. Any help appreciated.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Sidsy
  • 39
  • 4
  • 2
    "Stops" means crash or hang? Do you run Release or Debug build? What kind of application? – aepot Sep 12 '20 at 15:59
  • @aepot I am running Debug build, it basically exits without any exceptions or errors. – Sidsy Sep 12 '20 at 16:07
  • 2
    What about the last question? WPF? I'm asking because the app can't stop without Exception here but you don't see it. Is there "Just my code" debugger enabled, is there "optimize code" checked, "define TRACE and DEBUG" constants? Open build configuration and see it. While you're running the app there's an Output gray window in Visual Studio, look there, the Exception may appear there. – aepot Sep 12 '20 at 16:15
  • Open menu "Build - Batch build", check all configurations and press "Clean". Then run again. This is the workaround if something wrong with build process. – aepot Sep 12 '20 at 16:17
  • Ah, Console. Add `Console.ReadKey()` as last line in `Main` method, it will prevent app from closing. – aepot Sep 12 '20 at 16:20
  • @aeport But, it doesn't process all the items. I am trying to figure out that issue first. – Sidsy Sep 12 '20 at 16:21
  • Was ReadKey helped? – aepot Sep 12 '20 at 16:41
  • @aepot I added the console.readKey, checked the Output window, nothing stands out. It says the thread exited with exit code 0. But the Console.ReadKey() never gets called? – Sidsy Sep 12 '20 at 16:41
  • 1
    Looks like the problem is outside of the shown code. You have `throw ex;`, where are you catching it? Show more code. – aepot Sep 12 '20 at 16:49
  • 2
    `throw ex;` should be `throw;` – Aluan Haddad Sep 12 '20 at 16:57
  • Also [try this](https://stackoverflow.com/a/54084067/12888024) – aepot Sep 12 '20 at 16:59
  • How do you call the `LaunchComparators` method? Also are there any `async void` methods in your application? Finally I would suggest running it without the debugger attached (with Ctrl+F5). – Theodor Zoulias Sep 13 '20 at 21:09

1 Answers1

-1

Parallel For and ForEach operations do not re-throw exception that occur inside the loop. If you don't add exception handling inside the loop the exceptions are lost and the loop just terminates.

Here is a link to an article on how to handle exception inside of Parallel processing loops:

https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-handle-exceptions-in-parallel-loops