I need help with Parallel.Foreach loop in C#. I have a feeling that sometimes "result" tuple is rewritten by other parallel thread so there is no correct output. It doesn't occur often, but sometimes it spits out bad result.
Scenario: Network device is accessible, everything works fine output is stored in the "result" tuple. But then I when I work with the "result" further, the tuple is sometimes empty even though the output was stored there. Could it be that other thread in parallel processing is erasing the "result" ? Can I make it more threadsafe ?
Tuple <string, int> result;
/*loop through all hosts and execute commands -> In paralel*/
Parallel.ForEach(devices, (hostname, state) =>
{
//because we have multithreading every thread must differentiate -> based on hostname (first value in list) - key is also hostname
device_checked_with_all_parameters.Add(cleaned_hostname, new List<string> { DateTime.Now.ToString("d.M.yyyy HH:mm:ss"), cleaned_hostname, "", "", "", "" });
//if stop is pushed break all threads
if (token.IsCancellationRequested)
{
canceled_loop = 1;
state.Break();
}
//PERFORM ACTION ON DEVICE - STORE OUTPUT IN RESULT
result = core.connect_to_device_and_perform_actions(device_checked_with_all_parameters[cleaned_hostname][1], config_vlan_ids_based_on_type);
});