We have created a DotNet Core Windows Service where we are performing a scheduled tasks to create CSV files returned from database results.
We have implemented
Parallel.Invoke
with a
MaxDegreeOfParallelism
set to 3. I want to check whether my piece of code is thread safe or not.
I have invoked method
WriteListToFtp
with different parameters in parallel which fetches the data from database using Dapper to an IEnumerable result.
Can anyone help me know if the use of Parallel.Invoke will create a non thread safety scenario, as while testing all the results are returned correctly without any non thread safe exceptions.
What I have tried:
Parallel.Invoke(5,
() => { WriteListToFtp("SPExp_GetCountriesAll", "FLGetCountriesAll"); },
() => { WriteListToFtp("SPExp_GetStatesAll", "FLGetStatesAll"); },
() => { WriteListToFtp("SPExp_GetCitiesAll", "FLGetCitiesAll"); },
() => { WriteListToFtp("SPExp_GetTownsAll", "FLGetTownsAll"); },
() => { WriteListToFtp("SPExp_GetSendersTypeOfIdAll", "FLGetSenderStypeOfIdAll"); }
);
private void WriteListToFtp(string storedProcedure, string filename)
{
try
{
IEnumerable<dynamic> results = _primaryDBUtility.ExecuteQuery(null, storedProcedure);
if (results != null && results.Count() > 0)
{
_processCSV.IEnumerableToCSV(results, _srcFtpFilePath, _destFtpFilePath, filename);
}
}
catch (Exception ex)
{
throw new Exception($"The job failed. Task: CatalogExport | FileName: {filename} | Error: {ex.Message}");
}
}