执行异步任务,并记录时间
var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); try { DataTable dt = DbHelperSQL.Query("select CampaignCode from Test.dbo.[F2SMS04] where ISNULL(Mobile,'') != '' group by CampaignCode").Tables[0]; if (dt != null && dt.Rows.Count > 0) { Task[] tasks = new Task[dt.Rows.Count]; for (int i = dt.Rows.Count - 1; i >= 0; i--) { DataRow dataRow = dt.Rows[i]; string str = dataRow["CampaignCode"].ToString(); var task1 = Task.Factory.StartNew(() => { ShowData("F1", "[F2SMS04]", str); }); tasks[i] = task1; } Task.WaitAll(tasks ); } //var task1 = Task.Factory.StartNew(() => { ShowData("F1", "[F2SMS04]"); }); //var task2 = Task.Factory.StartNew(() => { ShowData("F2", "[27F2Y1983]"); }); //var task3 = Task.Factory.StartNew(() => { ShowData("F3", "[33F3N1850]"); }); //Task.WaitAll(new[] { task1 }); gvResult.DataSource = dtShow; gvResult.DataBind(); } catch (Exception ex) { Response.Write("任务异常:" + ex.Message + "<br/>"); } // cde.Wait(); stopwatch.Stop(); lblTip.Text = "ShortUrl生成成功" + "生成时间:" + stopwatch.ElapsedMilliseconds;