Многопоточность для более чем одной строки соединения в C#
Я хочу реализовать многопоточность для строки соединения, которая будет извлекать больше строки соединения из файла, она работает, но занимает так много времени, поэтому я хочу улучшить свой код:
Что я уже пробовал:
using System.Configuration; using System.Data; using System.Data.SqlClient; using MySql.Data.MySqlClient; namespace ConsoleApplication1 { public class Program { static void Main(string[] args) { Program p = new Program(); p.WithTPLAndParallelOptions(); } private object threadLock = new object(); public void ConnectDBAndExecuteQueryWithLock(string connectionString) { lock (threadLock) { try { string mysqlQuery = "SELECT PS_CD,COUNT(*) AS OFFLINE_FIR_COUNT FROM t_fir_registration WHERE state_cd=18 AND lang_cd=99 GROUP BY PS_CD"; //string connectionString = @"Server=.\SQLEXPRESS;Database=AUTODATA;Password=abc@123;User ID=sa"; using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); using (MySqlCommand command = new MySqlCommand(mysqlQuery, connection)) { command.CommandTimeout = 80; command.ExecuteNonQuery(); Console.WriteLine("Executed Thread.. " + Thread.CurrentThread.Name); } } } catch (Exception e) { Console.WriteLine(e.Message); } } } public void WithTPLAndParallelOptions() { string cctns_offline_DBConnectionString = ConfigurationManager.ConnectionStrings["cctns_offline_DBConnectionString"].ConnectionString; string[] filePaths = Directory.GetFiles(ConfigurationManager.AppSettings["filePath"].ToString(), "*.properties", SearchOption.TopDirectoryOnly); int ThreadCount = filePaths.Length; ParallelOptions options = new ParallelOptions(); options.MaxDegreeOfParallelism = 5; //Create Parallel.For to execute the task Parallel.For(0, ThreadCount, options, i => { foreach (string fileName in filePaths) { // i = i + 1; // Read a text file using StreamReader using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName)) { String line; while ((line = sr.ReadLine()) != null) { if (line.StartsWith("db.url")) { string[] PS_CD = fileName.Split(new[] { "\\", ".", "_" }, StringSplitOptions.None); string[] ip_address = line.Split(new[] { ";", "//", "/", ":" }, StringSplitOptions.None); if (ip_address[3].ToString() != string.Empty && ip_address[3] != "SQL2K8CLUSTER") { string connstringrpl = cctns_offline_DBConnectionString.Replace("PS_IP", ip_address[3].ToString()); ConnectDBAndExecuteQueryWithLock(connstringrpl); } } } } } }); } } }