How to make function ExecuteNonQuery to use multi thread to prevent computer hangs ? RRS feed

  • Question

  • I using function ExecuteNonQuery() for insert and update and delete using c# .

    This function using to make insert and update more thousand of records may be 10000 records so that computer hangs

    to solve this problem i need to modify function

    below to allow multi threads 

    so that i need to modify function to allow multi threads 

    How i do that in code 

    public static int ExecuteNonQuery(string sql, DbParameter[] dbprmParameters = null)
            return ExecuteNonQuery(sql, null, dbprmParameters);
        public static int ExecuteNonQuery(string sql, IDbConnection dbConnection, DbParameter[] @params = null)
            int RecordsCount = 0;
            lock (synObj)
                if (cmd.CommandTimeout < 360)
                    cmd.CommandTimeout = 360;
                if (sql == "") return 0;
                sql = AnalyizeBooleanFields(sql);
                cmd.CommandText = sql;
                if (@params != null)
                    for (int i = 0; i < @params.Length; i++)
                if (dbConnection == null)
                    if (WithTransaction)
                        dbConnection = BeginTransaction();
                        dbConnection = InitializeConnection();
                if (dbConnection.State != ConnectionState.Open) dbConnection.Open();
                if (WithTransaction) cmd.Transaction = _transaction;
                cmd.Connection = dbConnection;
                RecordsCount = cmd.ExecuteNonQuery();
                if (!WithTransaction) dbConnection.Close();
            return RecordsCount;

    How to edit function above to allow multi threads ?

    I work in visual studio 2010 windows form application with sql server 2012 .

    Saturday, April 14, 2018 12:50 AM

All replies

  • Hello,

    Is there a reason you are not using MERGE or BULK INSERT ? Seems prudent to go with one of these rather than doing thousands or more inserts or updates.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, April 14, 2018 1:04 AM
  • yes 

    this function i using in my windows form application more time 

    so that i prefer to use multi thread 

    Saturday, April 14, 2018 1:15 AM
  • https://visualstudiomagazine.com/articles/2011/03/24/wccsp_asynchronous-programming.aspx


    • Proposed as answer by Fei Hu Monday, April 16, 2018 5:06 AM
    Saturday, April 14, 2018 1:59 AM
  • Hello ahmedbarbary,

    Lock keywords ensures a critical section of code is executed by only one thread. It make no sense that you start multi thread and let them wait. To prevent computer hangs you have the below options.

    1. Create a thread to run heavy job.

    2. Dispatch job to asynchronous execute by Task.Run.

    3. Use async/await keyword to build responsive program.

    Best Regards,

    Neil Hu 

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 16, 2018 5:06 AM