简体   繁体   English

为什么我的Task.ContinueWith不等待定义时间

[英]Why my Task.ContinueWith not wait the define time

I am using this method to open specific work with concurrent Threads from my Main UI thread: 我正在使用此方法从主UI线程中打开并发线程的特定工作:

    private List<MyData> MyCollection;
    private static CancellationTokenSource _tokenSource;

        private void Start()
        {
            int concurrentThread = (int)nudConcurrentFiles.Value;
            int loops = (int)nudLoops.Value;
            var token = _tokenSource.Token;
            Task.Factory.StartNew(() =>
            {
                try
                {
                    while (Iteration.LoopFinished < loops)
                    {
                        Parallel.ForEach(PcapList.Files,
                        new ParallelOptions
                        {
                            MaxDegreeOfParallelism = concurrentThread //limit number of parallel threads 
                        },
                        File=>
                        {
                            if (token.IsCancellationRequested)
                                return;
                            //do work...
                        });

                        Iteration.LoopFinished++;

                        Task.Delay(10000).ContinueWith(
                           t =>
                           {

                           }, _tokenSource.Token);
                    }
                }
                catch (Exception e)
                { }

            }, _tokenSource.Token,
           TaskCreationOptions.None,
           TaskScheduler.Default).ContinueWith(
                t =>
                {

                }
            );
        }

The problem is that after loop i want to wait 10 secons and Task.Delay(10000).ContinueWith not waiting this 10 seconds but start immedietly another loop. 问题是循环后,我想等待10个secons和Task.Delay(10000).ContinueWith不等待这10秒钟,而是立即开始另一个循环。

You need to call Wait() method in order to execute the task 您需要调用Wait()方法以执行任务

Task.Delay(10000).ContinueWith(
t =>
{
}, _tokenSource.Token).Wait();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM