Click here to Skip to main content
14,423,178 members
Rate this:
Please Sign up or sign in to vote.
See more:
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"); }                             
                           );



/// <summary>
        /// Method to fetch the results and write it to FTP.
        /// </summary>
        /// <param name="storedProcedure"></param>
        /// <param name="csvName"></param>
        /// <param name="parameters"></param>
        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}");
            }
        }
Posted
Comments
Kornfeld Eliyahu Peter 13-Jan-20 8:41am
   
Parallel.Invoke itself has nothing to do with thread safety... You have to check the classes/methods you are using inside it...
Neetesh_Ch 13-Jan-20 12:34pm
   
Yes, you are right but the way I have implemented Parallel.Invoke, will the method which is being called in concurrent thread will break parallel execution ?
Kornfeld Eliyahu Peter 13-Jan-20 13:34pm
   
So you actually ask if the methods will run parallel in real?
I see no reason not to - except that I'm not sure you defined the method properly... It should be static so Invoke would find and execute...
An other point is the _ parameters (like _primaryDBUtility), they must be thread safe to access them without problem...
Neetesh_Ch 14-Jan-20 0:12am
   
Below is the method which would be called in parallel threads.

public IEnumerable<dynamic> ExecuteQuery(object listParameters, string storedProcedure)
{
IEnumerable<dynamic> results = null;

using (IDbConnection conn = new SqlConnection(connectionString))
{
results = conn.Query(sql: storedProcedure, param: listParameters, commandType: CommandType.StoredProcedure);
}

return results;
}

Could you please help me if this method should be made thread safe and if yes, which is the best way to implement thread safe. Most of the articles on internet suggest to use Lock, but my question is if lock is used then the next threads will be stopped before the thread which is holding the resources has completed with its use. If this is the case then what is the use of invoking parallel threads if each threads has to wait for their turn to execute.
Kornfeld Eliyahu Peter 14-Jan-20 1:56am
   
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/asynchronous-programming?view=netframework-4.8
Neetesh_Ch 14-Jan-20 4:20am
   
Thanks for your help, this will help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100