Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
Multithreading 如何在C中终止或中止Task.Run或Task.Factory.StartNew()#_Multithreading_Winforms_C# 4.0 - Fatal编程技术网

Multithreading 如何在C中终止或中止Task.Run或Task.Factory.StartNew()#

Multithreading 如何在C中终止或中止Task.Run或Task.Factory.StartNew()#,multithreading,winforms,c#-4.0,Multithreading,Winforms,C# 4.0,在我的ForEach循环中运行 要求: 我将被迫手动终止线程 我有一个按钮,可以在其中启动和停止此线程或任务。在For循环中运行 问题 我的问题是当我启动Task.Run方法时,它正在运行,但当我尝试停止使用CancellationTokenSource或runningTaskThread.Abort()时它不会杀人。当我启动新任务时,它就停止了。在那个时候它运行旧线程,所以每次启动进程它都会变成多线程 代码: 下面是我的开始线程代码 var messages = rootObject.Mul

在我的
ForEach
循环中运行

要求: 我将被迫手动终止线程 我有一个按钮,可以在其中启动和停止此
线程
任务。在For循环中运行

问题 我的问题是当我启动Task.Run方法时,它正在运行,但当我尝试停止使用
CancellationTokenSource
runningTaskThread.Abort()时它不会杀人。当我启动新任务时,它就停止了。在那个时候它运行旧线程,所以每次启动进程它都会变成多线程

代码:

下面是我的开始线程代码

 var messages = rootObject.MultiQData.Messages.Where(m => m.TimeStamp > DateTime.Now).OrderBy(x => x.TimeStamp).ToList();
                                //Simulate MultiQ file in BackGroud 
                                if (messages.Count > 0)
                                {
                                    cancellationTokenSource = new CancellationTokenSource();
                                    cancellationToken = cancellationTokenSource.Token;
                                    Task.Factory.StartNew(
                                        () =>
                                        {
                                            runningTaskThread = Thread.CurrentThread;
                                            messages.ForEach(
                                                m => SetUpTimer(m, rootObject.MultiQData.Connection.FleetNo));
                                        }, cancellationToken);
                                }
用于停止任务。运行

 if (cancellationTokenSource != null)
            {
                if (cancellationToken.IsCancellationRequested)
                    return;
                else
                    cancellationTokenSource.Cancel();
            }
我还将
Thread
Thread.Abort一起使用,但它不起作用


请帮助解决此问题

我使用
timer.Stop()、timer.Dispose()获得了解决方案
。在创建线程时,我调用
SetUpTimer
,这个SetUpTimer我创建了多个计时器

所以在停止线程的调用中,我已经为我配置了计时器和它的工作

参考请参见下面的代码

private void SetUpTimer(Message message, string fleetNo)
        {
            var ts = new MessageTimer();

            var interval = (message.TimeStamp - DateTime.Now).TotalMilliseconds;
            interval = interval <= 0 ? 100 : interval;

            ts.MessageWrapper = new MessageWrapper(message, fleetNo);
            ts.Interval = interval;
            ts.Elapsed += ts_Elapsed;
            ts.Start();

            //Add timer in to the lost for disposing timer at time of stop Simulation  
            lsTimers.Add(ts);
        }
    private void StopTask()
    {
        try
        {
            // Attempt to cancel the task politely
            if (cancellationTokenSource != null)
            {
                if (cancellationToken.IsCancellationRequested)
                    return;
                else
                    cancellationTokenSource.Cancel();
            }

            //Stop All Timer 
            foreach (var timer in lsTimers)
            {
                timer.Stop();
                timer.Dispose();
            }
        }
        catch (Exception ex)
        {
            errorLogger.Error("Error while Stop simulation :", ex);
        }

    }