Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中实现健壮的线程监控?_C#_Multithreading - Fatal编程技术网

如何在C#中实现健壮的线程监控?

如何在C#中实现健壮的线程监控?,c#,multithreading,C#,Multithreading,我有两个并行运行的任务,下面是任务信息。 任务1-启动并运行应用程序 任务2-监视应用程序运行持续时间。如果超过30分钟,发出任务1应用程序的停止命令,并重新启动两个任务 任务1应用程序位重,在长时间运行期间容易出现内存泄漏 我在问,在这种情况下,我们如何实现健壮的线程解决方案 using QuickTest; using System; using System.Threading; using System.Threading.Tasks; name

我有两个并行运行的任务,下面是任务信息。 任务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();
            }
        }
    }
}
使用QuickTest;
使用制度;
使用系统线程;
使用System.Threading.Tasks;
名称空间TaskParallelExample
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
调用(RunApplication、MonitorApplication);
}
私有静态应用程序()
{
应用程序uftInstance=新应用程序();
uftInstance.Launch();
QuickTest.Test uftTestInstance=uftInstance.Test;
打开(@“C:\Tasks\Test1”);
uftInstance.Test.Run();//它的运行时间可能超过30分钟,也可能少于30分钟。它超过了从监视器应用程序计算得出的30分钟。
}
私有静态void监视器应用程序()
{
应用程序uftInstance=新应用程序();
尝试
{
DateTime uftTestRunMonitor=DateTime.Now;
int runningTime=(DateTime.Now-uftTestRunMonitor).Minutes;

而(runningTime您是否可以使用超时设置为30分钟的
CancellationTokenSource

var stopAfter30Mins = new CancellationTokenSource(TimeSpan.FromMinutes(30));
然后将其传递给worker方法:

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.
    }
}

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

是否可以使用超时设置为30分钟的
CancellationTokenSource

var stopAfter30Mins = new CancellationTokenSource(TimeSpan.FromMinutes(30));
然后将其传递给worker方法:

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.
    }
}
请注意,如果辅助任务无法定期检查取消状态,则没有可靠的方法来处理任务超时

System.Threading.Tasks.Task完成任务

CancellationTokenSource cts=新的CancellationTokenSource();
CancellationToken=cts.token;
Task myTask=Task.Factory.StartNew(()=>
{
对于(int i=0;i<2000;i++)
{
token.ThrowIfCancellationRequested();
//for循环的主体。
}
},代币);
//做点别的
//如果需要取消
cts.Cancel();
System.Threading.Tasks.Task完成任务

CancellationTokenSource cts=新的CancellationTokenSource();
CancellationToken=cts.token;
Task myTask=Task.Factory.StartNew(()=>
{
对于(int i=0;i<2000;i++)
{
token.ThrowIfCancellationRequested();
//for循环的主体。
}
},代币);
//做点别的
//如果需要取消
cts.Cancel();

我将使用取消令牌。任务2可以设置
令牌.cancel();
并且任务1正在检查
是否(令牌.IsCancelled)
类似于此。谢谢。但我无法验证任务2的状态,因为任务1应用程序将处于运行状态,并且在结束时会出现。我将使用取消令牌。任务2可以设置
令牌。取消();
并且任务1正在检查
是否(令牌.IsCancelled)
类似于此。谢谢。但我无法验证任务2的状态,因为任务1应用程序将处于运行状态,并在结束时显示。正如您所指出的。任务1应用程序将处于运行状态,直到运行结束后才会显示。我可以使用Parallel.Task并使用计时器监视任务2吗。如果达到30分钟,则发出stask 1应用程序的top命令。@Ram“发出停止命令”是什么意思?
CancellationTokenSource
执行“发出停止命令”(这是它存在的全部理由)。非合作停止线程的唯一方法是通过
thread.Abort()
,这非常不可靠(我们处理
thread.Abort()的任何使用)
作为bug)。我的意思是,不要停止线程,而是调用“stop命令”任务1应用程序的。正如您所指出的。任务1应用程序将处于运行状态,在运行结束之前不会出现。我可以使用Parallel.task并使用计时器监视任务2吗。如果达到30分钟,则发出任务1应用程序的停止命令。@Ram“发出停止命令”是什么意思?a
CancellationTokenSource
可以“发出停止命令”(这是它存在的全部理由)。非合作停止线程的唯一方法是通过
thread.Abort()
,这非常不健壮(我们将
thread.Abort()
的任何使用视为一个bug)。我的意思是,不要停止线程,而是调用Task1应用程序的“stop command”。