如何使用manualResetEvent或任何其他建议停止所有线程来停止所有线程?

时间:2019-01-16 07:04:06

标签: c# multithreading windows-services manualresetevent

我正在使用10个线程,每个线程获取10条状态为null的记录,然后 将状态更新为“ Q”。现在将这些记录请求给不同的API,并在成功响应后将状态更新为“ C”,一切都很好。作为停止所有线程的停止器,我在(j> 30)ManualResetEven.Set()我检查条件后使用了ManualResetEvent

if (_stopper.WaitOne(0, false))
  {
     break;
  } 

要停止我的while循环并将状态为“ Q”的每个线程的其余记录的状态更新为null。在停止我将10个线程的所有记录的状态用“ Q”的状态更新为null时,问题是线程记录的记录不是从“ Q”状态更新为null吗?

这是我的代码:

 public void MainProcess()
    {
        int totalThread = 10;
        Thread[] ThreadNum = new Thread[totalThread];
        DataTable dt = new DataTable();
        int j = 1;
        while (true)
        {
            if (_stopper.WaitOne(0, false))
            {
                lov.LogWriter("MainProcess stopper. |" + dt.Rows[0]["que_batch"].ToString());
                string var = sp_Queue_upd(dt.Rows[0]["que_batch"].ToString(), "Q");
                lov.LogWriter("MainProcess stopper sp_Queue_upd " + var + " stopper call, current number is " + que_BatchNumberIndex + " Batch No. :" + que_BatchNumber + ": mainthread End of While");//+ (string)argArray.GetValue(1));

                break;
            }
            if (j > 30)
            {
                _stopper.Set();
                lov.LogWriter("MainProcess stopper.Set");
            }
            if (threadCount.ThreadCount < totalThread && threadCount.ThreadCount >= 0)
            {
                if (ThreadNum[threadCount.ThreadCount] == null)
                {
                    dt = sp_get_entry_spool();
                    if (dt.Rows.Count <= 0)
                    {
                        Thread.Sleep(1000);
                        continue;
                    }
                    object[] objArr = { dt, threadCount.ThreadCount.ToString(), dt.Rows[0]["que_batch"].ToString() };
                    ThreadNum[threadCount.ThreadCount] = new Thread(new ParameterizedThreadStart(Process));
                    ThreadNum[threadCount.ThreadCount].Name = dt.Rows[0]["que_batch"].ToString();
                    ThreadNum[threadCount.ThreadCount].IsBackground = true;
                    ThreadNum[threadCount.ThreadCount].SetApartmentState(ApartmentState.MTA);
                    ThreadNum[threadCount.ThreadCount].Start(objArr);
                    lov.LogWriter("thread Start: Batch No. :" + dt.Rows[0]["que_batch"].ToString() + "|Actual Batch :" + j + "|ThreadCount" + threadCount.ThreadCount.ToString());

                }
                else if (!ThreadNum[threadCount.ThreadCount].IsAlive)
                {
                    dt = sp_get_entry_spool();
                    if (dt.Rows.Count <= 0)
                    {
                        Thread.Sleep(1000);
                        continue;
                    }
                    object[] objArr = new object[3] { dt, threadCount.ThreadCount.ToString(), dt.Rows[0]["que_batch"].ToString() };
                    ThreadNum[threadCount.ThreadCount] = new Thread(new ParameterizedThreadStart(Process));
                    ThreadNum[threadCount.ThreadCount].Name = dt.Rows[0]["que_batch"].ToString();
                    ThreadNum[threadCount.ThreadCount].IsBackground = true;
                    ThreadNum[threadCount.ThreadCount].SetApartmentState(ApartmentState.MTA);
                    ThreadNum[threadCount.ThreadCount].Start(objArr);
                    lov.LogWriter("thread Start: Batch No. :" + dt.Rows[0]["que_batch"].ToString() + "|Actual Batch :" + j + "|ThreadCount" + threadCount.ThreadCount.ToString());
                    j++;
                }
                threadCount.ThreadCount++;

            }
            else if (threadCount.ThreadCount >= totalThread)
            {
                threadCount.ThreadCount = 0;
            }
        }
        if (statusQueue)
        {
            string var = sp_Queue_upd(que_BatchNumber, "Q");
            lov.LogWriter("Back to MainProcess" + var + " stopper call, current number is " + que_BatchNumberIndex + " Batch No. :" + que_BatchNumber + ": mainthread End of While" );//+ (string)argArray.GetValue(1)); 
        }
    }
    public void Process(object ob)
    {
        Array argArray = new object[3];
        argArray = (Array)ob;
        DataTable Data = (DataTable)argArray.GetValue(0);
        string var = "";
        try
        {
            Core_Incor objCore = null;
            int i = 0;
            for (i = 0; i < Data.Rows.Count; i++)
            {
                #region Loop
                var = "";
                try
                {
                    #region Loop
                    if (_stopper.WaitOne(0, false))
                    {   var = sp_Queue_upd(Data.Rows[i]["que_batch"].ToString(), "Q");
                        lov.LogWriter("sp_Queue_upd " + var + " stopper call, current number is " + i + " Batch No. :" + Data.Rows[i]["que_batch"].ToString() + ":" + (string)argArray.GetValue(1));

                        break;
                    }

                    if (Data.Rows[i]["mesg_type"].ToString() == "CQ")
                    { // mark status to "C" }
                    else if (Data.Rows[i]["mesg_type"].ToString() == "IN")
                    { // mark status to "C" }
                    else if (Data.Rows[i]["mesg_type"].ToString() == "CH")
                    { // mark status to "C" }
                    else if (Data.Rows[i]["mesg_type"].ToString() == "IF")
                    { // mark status to "C" }
                    else if (Data.Rows[i]["mesg_type"].ToString() == "IB")
                    { // mark status to "C" }
                    else if (Data.Rows[i]["mesg_type"].ToString() == "IB")
                    { // mark status to "C" }
                    else if (Data.Rows[i]["mesg_type"].ToString() == "BA")
                    { // mark status to "C" } 
                     else if (Data.Rows[i]["mesg_type"].ToString() == "RT")
                    { // mark status to "C" }
                    else if (Data.Rows[i]["mesg_type"].ToString() == "CA" || Data.Rows[i]["mesg_type"].ToString() == "UB")
                    { // mark status to "C" }

                }
                catch (Exception ex)
                {
                    InsertReponce("1", ex.Message, Data.Rows[i]["rowid"].ToString());
                }
                #endregion
            }
        }
        catch (Exception ex)
        {
            var = "1;MainException:FE Process :" + ex.Message;
            lov.LogWriter(var);
        }
    }

0 个答案:

没有答案