Have a look at the following code:
try
{
Parallel.Foreach(...)//50 Thread For Each Time
{
string str = MyMethod();
}
}
catch
{
}
public string MyMethod()
{
CookieContainer cookieJar = new CookieContainer();
// Some code that throws an exception.
}
How can I pass cookieJar to the catch block?
Note:
For some reason I can’t use global variables. I’m using Parallel.ForEach in try And I can’t control global variables inside MyMethod because of parallel changes.
You could catch the exception in
MyMethodand rethrow a new exception that will hold theCookieContainerobject: