3

我在使用 .NET 4.0 的任务并行库和 ADO.Net 时遇到问题。我试图通过为每个存储过程创建两个单独的任务来并行执行两个存储过程。场景是这样的。第一个存储过程需要 20 秒执行,另一个需要大约 12 秒。当我运行代码时,执行时间是这两个时间段的总和(即 20 + 12 = 32 秒) .这不是我所期望的。我假设当第一个存储过程完成执行时,第二个存储过程已经完成,因此执行的总时间应该在 20 秒左右。我也有观察到,在我的代码中,如果我将 ADO.Net 部分替换为

线程.睡眠(时间)

,我得到的结果是预期的,即如果我设置两个线程分别为 20 秒和 12 秒,程序将在 20 秒内执行。这与我尝试使用 ADO.Net 实现的行为相同。我还观察到,如果我有这些陈述,

IAsyncResult 结果1 = cmd1.BeginExecuteReader();
IDataReader reader1 = cmd1.EndExecuteReader(result1);

对于其中一个存储过程,我得到了预期的行为。但是,当我将其应用于另一个存储过程结果时,程序会按顺序执行。我对这种行为感到困惑。因此,请你们中的任何人帮助我实现我正在寻找的结果。我在这里附上完整的代码。请让我知道我哪里出错了。

 string connectionString1 = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
    string connectionString2 = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
    string connectionString3 = ConfigurationManager.ConnectionStrings["ConnectionString3"].ConnectionString;
    List<int> results = new List<int>();
    Dictionary<string, int> dict = new Dictionary<string, int>();

    List<SqlDataReader> rdrs = new List<SqlDataReader>();

    public List<int> returnSelect()
    {

        var taskIndex = -1;
        var start = DateTime.Now;
        Task[] tasks = new Task[]
        {
            Task.Factory.StartNew(() => PrintNumber1()),
            Task.Factory.StartNew(() => PrintNumber2()),
            Task.Factory.StartNew(() => PrintNumber3())
        };
        Dictionary<int, DateTime> taskList = new Dictionary<int, DateTime>();
        while (tasks.Length > 0)
        {
            taskIndex = Task.WaitAny(tasks);
            for (int i = 0; i < tasks.Count(); i++)
            {
                Console.Write("Array {0} | ", tasks[i].Id);

            }
            taskList.Add(tasks[taskIndex].Id, DateTime.Now);
            tasks = tasks.Where((t) => t != tasks[taskIndex]).ToArray();
        }
        var end = DateTime.Now;
        var diff = end - start;
        int count = rdrs.Count;
        int dcount = dict.Count;
        return results;

    }

    //19
    public void PrintNumber1()
    {
        SqlConnection con1 = new SqlConnection(connectionString1);
        {
            var start = DateTime.Now;
            var copen = DateTime.Now;

            var cclose = DateTime.Now;
            int exSec = (cclose - copen).Seconds;
            SqlCommand cmd1 = new SqlCommand("PS_Brand_level", con1);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.AddWithValue("@startdate", "6/25/2012");
            cmd1.Parameters.AddWithValue("@enddate", "11/18/2012");
            cmd1.Parameters.AddWithValue("@subbrands", "mybrands");
            cmd1.Parameters.AddWithValue("@count", 11);
            cmd1.Parameters.AddWithValue("@category", "a");
            con1.Open();
            IAsyncResult result1 = cmd1.BeginExecuteReader();
            IDataReader reader1 = cmd1.EndExecuteReader(result1);
            var end = DateTime.Now;
            dict.Add((end - start).ToString(), 1);
        }

        /*var start = DateTime.Now;
        Thread.Sleep(19000);
        var end = DateTime.Now;
        dict.Add((end - start).ToString(), 1);*/
    }


    //9
    public void PrintNumber2()
    {

        SqlConnection con2 = new SqlConnection(connectionString2);
        {
            var start = DateTime.Now;
            var copen = DateTime.Now;
            con2.Open();
            var cclose = DateTime.Now;
            int exSec = (cclose - copen).Seconds;
            DataSet dtPSPercentage = new DataSet();
            SqlCommand cmd2 = new SqlCommand("Ps_Percentage", con2);
            cmd2.CommandType = CommandType.StoredProcedure;
            cmd2.CommandTimeout = 100;
            cmd2.Parameters.AddWithValue("@startdate", "6/25/2012");
            cmd2.Parameters.AddWithValue("@enddate", "11/18/2012");
            cmd2.Parameters.AddWithValue("@subbrands", "mybrands");
            cmd2.Parameters.AddWithValue("@category", "a");

            IAsyncResult result2 = cmd2.BeginExecuteReader();
            IDataReader reader2 = cmd2.EndExecuteReader(result2);
            var end = DateTime.Now;
            dict.Add((end - start).ToString(), 2);
        }

        /*var start = DateTime.Now;
        Thread.Sleep(9000);
        var end = DateTime.Now;
        dict.Add((end - start).ToString(), 2);*/
    }

    //12
    public void PrintNumber3()
    {

        SqlConnection con3 = new SqlConnection(connectionString3);
        {
            var start = DateTime.Now;
            var copen = DateTime.Now;
            //con3.Open();
            var cclose = DateTime.Now;
            int exSec = (cclose - copen).Seconds;
            SqlCommand cmd1 = new SqlCommand("Ps_Aggregated", con3);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.AddWithValue("@startdate", "6/25/2012");
            cmd1.Parameters.AddWithValue("@enddate", "11/18/2012");
            cmd1.Parameters.AddWithValue("@subbrands", "mybrands");
            cmd1.Parameters.AddWithValue("@category", "a");

           // IAsyncResult result1 = cmd1.BeginExecuteReader(CommandBehavior.SingleRow);
            //SqlDataReader reader1 = cmd1.EndExecuteReader(result1);
            var end = DateTime.Now;
            dict.Add((end - start).ToString(), 3);


        }

        /*var start = DateTime.Now;
        Thread.Sleep(12000);
        var end = DateTime.Now;
        dict.Add((end - start).ToString(), 3);*/


    }

感谢和问候

4

0 回答 0