我怎么知道Parallel.Foreach创建的线程是否仍在运行? [英] How can i know whether threads created by Parallel.Foreach are still running or not?

查看:102
本文介绍了我怎么知道Parallel.Foreach创建的线程是否仍在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道Parallel.Foreach创建的线程是否仍在运行?

因为如果任何一个线程仍在运行,那么我需要执行一些操作.

以下代码可能是帮助

How can i know whether threads created by Parallel.Foreach are still running or not?

Because if any one of the threads are still running, then i need to do some operation.

The below code might be help

Parallel.ForEach(GetProcess(), ExecuteProcess);

public IEnumerable<lucent.spat3g.dsm.bllcommon.processmodule> GetProcess()
        {
            Lucent.SPAT3G.DSM.BLLCommon.ProcessModule process = null;
            while (true)
            {
                process = _DDMatrix.GetReadyProcess();
                if (process == null)
                {
                    if (RunningProcessCount == 0)
                    {
                        Logger.Write("no process is ready AND no process is running.hence, processing over", QdalDsmConstants.LOGGER_CATEGORY_BLL);
                        break;
                    }
                    else
                        continue;
                }

                yield return process;
            }
        }

private void ExecuteProcess(Lucent.SPAT3G.DSM.BLLCommon.ProcessModule process)
        {
            // update jobtime
            RunningProcessCount++;
            CommonRegistryQuery regQuery = new CommonRegistryQuery();
            regQuery.WriteRegistry(Registry.LocalMachine, QdalDsmConstants.REG_KEY_DSM_BASE, QdalDsmConstants.REG_VAL_DSM_JOB_TIME, DateTime.Now.ToString("yyyyMMddhhmmss"));

            Logger.Write("launching process on thread 1: " + process.Id, QdalDsmConstants.LOGGER_CATEGORY_BLL);
            _DDMatrix.UpdateProcessStatus(process.Id, ProcessStatus.Running);
            PECommonWrap processWrap = new PECommonWrap(_DSMConfig, _SprMeta, _QidConfig, process);

            try
            {
                Logger.Write("Executing process " + process.Id, QdalDsmConstants.LOGGER_CATEGORY_BLL);
                bool result = ExecuteProcess(processWrap);
                if (result)
                {
                    _DDMatrix.UpdateProcessStatus(process.Id, ProcessStatus.Done);
                    Logger.Write("Process " + process.Id + " completed successfully", QdalDsmConstants.LOGGER_CATEGORY_BLL);
                }
                else
                {
                    _DDMatrix.UpdateProcessStatus(process.Id, ProcessStatus.Error);
                    Logger.Write("Process " + process.Id + " completed unsuccessfully", QdalDsmConstants.LOGGER_CATEGORY_BLL_ERROR);
                }
            }
            catch (Exception ex)
            {
                Logger.Write("failed in bwProcess1_DoWork, e:" + ex.Message, QdalDsmConstants.LOGGER_CATEGORY_BLL_ERROR);
                _DDMatrix.UpdateProcessStatus(process.Id, ProcessStatus.Error);
            }

            Logger.Write("runworker compeleted", QdalDsmConstants.LOGGER_CATEGORY_BLL);
            // update available file status
            _DDMatrix.UpdateFileStatus(new DirectoryInfo(_DSMConfig.WorkAreaDataIn.ToString()));
            _DDMatrix.UpdateFileStatus(new DirectoryInfo(_DSMConfig.WorkAreaDataOut.ToString()));
            RunningProcessCount--;
        }



现在,我通过保留一个名为RunningProcessCount的变量来进行手动控制,您是否知道您属于Parallel.Foreach的任何线程仍在运行?

谢谢invvance

哈里

[edit]已添加代码块-OriginalGriff [/edit]



Now im controlling manually by keeping the one variable called RunningProcessCount, Is there any way yo know that any of the threads that belongs to Parallel.Foreach is still running?

Thanks inadvance

Hari

[edit]Code block added - OriginalGriff[/edit]

推荐答案

您可以使用任何集合来获取正在运行的线程的列表.
为了简单起见,在这里我使用了一个字符串.但是您可以使用自己的对象列表.

You can use any collection to get a list of your running threads.
For simplicity here I used a string. But you could use a list of your own objects.

string RunningThreads = string.Empty;
object LockObject = new Object();

Parallel.ForEach("ABCDEFGHIJKLMNOP", (c, i) =>
{
    lock (LockObject)
    {
        RunningThreads += c.ToString();
        System.Console.WriteLine("Starting thread " + c+ ". Running threads: " + RunningThreads);
    }

    Thread.Sleep(1000);

    lock (LockObject)
    {
        RunningThreads = RunningThreads.Replace(c.ToString(), "");
        System.Console.WriteLine("Ending thread   " + c + ". Running threads: " + RunningThreads);
    }
});
System.Console.WriteLine("The End");
System.Console.WriteLine("Running threads: " + RunningThreads + " (should be empty)");


输出


Output

Starting thread A. Running threads: A
Starting thread B. Running threads: AB
Starting thread C. Running threads: ABC
Starting thread D. Running threads: ABCD
Starting thread E. Running threads: ABCDE
Ending thread   A. Running threads: BCDE
Starting thread F. Running threads: BCDEF
Ending thread   B. Running threads: CDEF
Starting thread G. Running threads: CDEFG
Ending thread   C. Running threads: DEFG
Starting thread H. Running threads: DEFGH
Ending thread   D. Running threads: EFGH
Starting thread I. Running threads: EFGHI
Ending thread   E. Running threads: FGHI
Starting thread J. Running threads: FGHIJ
Starting thread K. Running threads: FGHIJK
Ending thread   F. Running threads: GHIJK
Starting thread L. Running threads: GHIJKL
Ending thread   G. Running threads: HIJKL
Ending thread   H. Running threads: IJKL
Starting thread N. Running threads: IJKLN
Starting thread M. Running threads: IJKLNM
Ending thread   I. Running threads: JKLNM
Starting thread O. Running threads: JKLNMO
Ending thread   J. Running threads: KLNMO
Starting thread P. Running threads: KLNMOP
Ending thread   K. Running threads: LNMOP
Ending thread   L. Running threads: NMOP
Ending thread   N. Running threads: MOP
Ending thread   M. Running threads: OP
Ending thread   P. Running threads: O
Ending thread   O. Running threads:
The End
Running threads:  (should be empty)


Paraller.ForEach方法将等待所有子线程完成.
在下面的代码中,结束"将仅在所有ForEach函数均已完成时出现.
The Paraller.ForEach method waits till all the children threads complete.
In the code below ''The End'' will only appear when all the ForEach functions have been completed.
Parallel.ForEach("ABC", (c) =>
{
    Console.WriteLine("Starting " + c);
    Thread.Sleep(1000);
    Console.WriteLine("Ending " + c);
});
Console.WriteLine("The End");


输出:


Output:

Starting A
Starting C
Starting B
Ending C
Ending A
Ending B
The End


我错过了什么吗?


Am I missing something?


这篇关于我怎么知道Parallel.Foreach创建的线程是否仍在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆