如何在循环外停止Parallel for循环过程?

时间:2015-11-17 12:17:03

标签: c# asp.net parallel.for

我从button click(Start Button)开始并行for循环过程。并行过程进展顺利,但当我移动另一页时,我返回当前的并行流程页面,流程进展顺利。

我尝试从另一个button click(button stop)停止并行进程。有可能吗?

protected void btnstart_Click(object sender, EventArgs e)
{
   try
   {
      Parallel.For(0, dtrecord.Rows.Count, pOptions1, (j, pls) =>
         {               
            if (Session["Stop"] != null)
            {
               pls.Stop();
               Session.Remove("Stop2");
               Session["ParallelStopped"] = 1;
            }
            else
            {
               Checkrecords(dtrecord, ImportFileID, j);
            }
         });
   }
   catch (Exception ex)
   {
      Log.Trace("Error occured in btnstop_Click");
      Log.Error(ex.Message);
   }
}

protected void btnstop_Click(object sender, EventArgs e)
{
   try
   {
      Session["Stop"] = 1;

      Session.Remove("start");
      Session["start"] = null;

      BindImportfileDetails();
      BindImportrecordsdetails(hidimid.Value);
      bindimportrecords();
      Getrecordstatuscount();
      Log.Trace("User has been stopping the import process");
   }
   catch (Exception ex)
   {
      Log.Trace("Error occured in btnstop_Click");
      Log.Error(ex.Message);
   }
}

0 个答案:

没有答案