Skip to main content
added 148 characters in body
Source Link
poppertech
  • 1.3k
  • 2
  • 9
  • 17

The following code can be used to verify the behavior discussed in the first part of Evk's answer:

 static void TryCatchFunction() { ConcurrentBag<string> bag = null; int numItemsInBag = 0; try { ErrorFunction(out bag); numItemsInBag = bag.Count; } catch (Exception) { numItemsInBag = bag.Count; } } static void ErrorFunction(out ConcurrentBag<string> bag) { string[] strings = new string[] { "1", "2", "3", "4", "5", "6" }; ConcurrentBag<string> inFunctionBag = new ConcurrentBag<string>(); bag = inFunctionBag; Parallel.ForEach(strings, (str, state) => { if (str == "2" || str == "4") { inFunctionBag.Add(str); throw new Exception(); } }); } 

The number of items in the bag varies between method calls on a dual core machine. This occurs because sometimes the Exception cancels execution of the other thread and other times both run until completion.

The following code can be used to verify the behavior discussed in the first part of Evk's answer:

 static void TryCatchFunction() { ConcurrentBag<string> bag = null; int numItemsInBag = 0; try { ErrorFunction(out bag); numItemsInBag = bag.Count; } catch (Exception) { numItemsInBag = bag.Count; } } static void ErrorFunction(out ConcurrentBag<string> bag) { string[] strings = new string[] { "1", "2", "3", "4", "5", "6" }; ConcurrentBag<string> inFunctionBag = new ConcurrentBag<string>(); bag = inFunctionBag; Parallel.ForEach(strings, (str, state) => { if (str == "2" || str == "4") { inFunctionBag.Add(str); throw new Exception(); } }); } 

The number of items in the bag varies between method calls.

The following code can be used to verify the behavior discussed in the first part of Evk's answer:

 static void TryCatchFunction() { ConcurrentBag<string> bag = null; int numItemsInBag = 0; try { ErrorFunction(out bag); numItemsInBag = bag.Count; } catch (Exception) { numItemsInBag = bag.Count; } } static void ErrorFunction(out ConcurrentBag<string> bag) { string[] strings = new string[] { "1", "2", "3", "4", "5", "6" }; ConcurrentBag<string> inFunctionBag = new ConcurrentBag<string>(); bag = inFunctionBag; Parallel.ForEach(strings, (str, state) => { if (str == "2" || str == "4") { inFunctionBag.Add(str); throw new Exception(); } }); } 

The number of items in the bag varies between method calls on a dual core machine. This occurs because sometimes the Exception cancels execution of the other thread and other times both run until completion.

Source Link
poppertech
  • 1.3k
  • 2
  • 9
  • 17

The following code can be used to verify the behavior discussed in the first part of Evk's answer:

 static void TryCatchFunction() { ConcurrentBag<string> bag = null; int numItemsInBag = 0; try { ErrorFunction(out bag); numItemsInBag = bag.Count; } catch (Exception) { numItemsInBag = bag.Count; } } static void ErrorFunction(out ConcurrentBag<string> bag) { string[] strings = new string[] { "1", "2", "3", "4", "5", "6" }; ConcurrentBag<string> inFunctionBag = new ConcurrentBag<string>(); bag = inFunctionBag; Parallel.ForEach(strings, (str, state) => { if (str == "2" || str == "4") { inFunctionBag.Add(str); throw new Exception(); } }); } 

The number of items in the bag varies between method calls.