【问题标题】:How to implement robust thread monitoring in C#?如何在 C# 中实现健壮的线程监控?
【发布时间】:2016-05-02 17:00:30
【问题描述】:

我有 2 个任务并行运行,这里是任务信息。 任务 1 - 启动并运行应用程序 任务 2 - 监控应用程序运行持续时间。如果超过 30 分钟,发出任务 1 应用程序的停止命令并重新启动两个任务。

任务 1 应用程序在较长时间运行期间有点重且容易发生内存泄漏。

我正在请求,我们如何针对这种情况实施强大的线程解决方案。

    using QuickTest;
    using System;
    using System.Threading;
    using System.Threading.Tasks;

    namespace TaskParallelExample
     {
     internal class Program
      {
       private static void Main(string[] args)
        {
        Parallel.Invoke(RunApplication, MonitorApplication);
    }

    private static void RunApplication()
    {
        Application uftInstance = new Application();
        uftInstance.Launch();
        QuickTest.Test uftTestInstance = uftInstance.Test;
        uftInstance.Open(@"C:\Tasks\Test1");
        uftInstance.Test.Run(); // It will may run more then 30 mins or less then also. It it exceeds 30 mins which is calculated from Monitor Application.
    }

    private static void MonitorApplication()
    {
        Application uftInstance = new Application();
        try
        {
            DateTime uftTestRunMonitor = DateTime.Now;
            int runningTime = (DateTime.Now - uftTestRunMonitor).Minutes;
            while (runningTime <= 30)
            {
                Thread.Sleep(5000);
                runningTime = (DateTime.Now - uftTestRunMonitor).Minutes;
                if (!uftInstance.Test.IsRunning)
                {
                    break;
                }
            }
        }
        catch (Exception exception)
        {
            //To-do
        }
        finally
        {
            if (uftInstance.Test.IsRunning)
            {
                //Assume it is still running and it is more then 30 mins
                uftInstance.Test.Stop();
                uftInstance.Test.Close();
                uftInstance.Quit();
            }
        }
    }
}

}

谢谢, 拉姆

【问题讨论】:

  • 我会使用取消令牌。任务 2 可以设置 token.Cancel(); 并且任务 1 正在检查 if (token.IsCancelled) 是否像这样。
  • 谢谢。但是我无法验证任务 2 的状态,因为任务 1 应用程序将处于运行状态,并且会在它结束时出来。

标签: c# multithreading


【解决方案1】:

您可以使用CancellationTokenSource 并将超时设置为 30 分钟吗?

var stopAfter30Mins = new CancellationTokenSource(TimeSpan.FromMinutes(30));

然后你会将它传递给你的工作方法:

var task = Task.Run(() => worker(stopAfter30Mins.Token), stopAfter30Mins.Token);

...

static void worker(CancellationToken cancellation)
{
    while (true)  // Or until work completed.
    {
        cancellation.ThrowIfCancellationRequested();
        Thread.Sleep(1000); // Simulate work.
    }
}

请注意,如果工作任务无法定期检查取消状态,则没有强大的方法来处理任务超时。

【讨论】:

  • 正是你指出的。任务 1 应用程序将处于运行状态,直到运行结束才会出现。我可以使用 Parallel.Task 并使用计时器监控任务 2。如果达到 30 分钟,则发出任务 1 应用程序的停止命令。
  • @Ram “发出停止命令”是什么意思? CancellationTokenSource 确实“发出停止命令”(这是它存在的全部理由)。非合作停止线程的唯一方法是通过Thread.Abort(),这非常不可靠(我们将任何使用Thread.Abort() 视为错误)。
  • 我的意思是,不要停止线程,而是调用任务 1 应用程序的“停止命令”。
【解决方案2】:

System.Threading.Tasks.Task 完成这项工作

   CancellationTokenSource cts = new CancellationTokenSource();
            CancellationToken token = cts.Token;
            Task myTask = Task.Factory.StartNew(() =>
           {
               for (int i = 0; i < 2000; i++)
               {
                   token.ThrowIfCancellationRequested();

                   // Body of for loop.
               }
           }, token);

            //Do sometohing else 
            //if cancel needed
            cts.Cancel();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多