没有UI线程的任务同步

时间:2011-07-01 16:19:45

标签: c# task task-parallel-library

在下面的代码中,我想同步报告任务列表的结果。这现在正在工作,因为task.Result会阻塞,直到任务完成。但是,task id = 3需要很长时间才能完成,并阻止所有其他已完成的任务报告其状态。

我认为我可以通过将报告(Console.Write)移动到.ContinueWith指令来完成此操作,但我没有UI线程,那么如何让TaskScheduler同步.ContinueWith任务呢?

我现在拥有的:

static void Main(string[] args)
{
    Console.WriteLine("Starting on {0}", Thread.CurrentThread.ManagedThreadId);

    var tasks = new List<Task<int>>();

    for (var i = 0; i < 10; i++)
    {
        var num = i;
        var t = Task<int>.Factory.StartNew(() =>
        {
           if (num == 3)
           {
               Thread.Sleep(20000);
           }
           Thread.Sleep(new Random(num).Next(1000, 5000));
           Console.WriteLine("Done {0} on {1}", num, Thread.CurrentThread.ManagedThreadId);
           return num;
        });
        tasks.Add(t);
    }

    foreach (var task in tasks)
    {
        Console.WriteLine("Completed {0} on {1}", task.Result, Thread.CurrentThread.ManagedThreadId);
    }

    Console.WriteLine("End of Main");
    Console.ReadKey();
}

我想转到这个或类似的东西,但我需要Console.Write(“Completed ...”)才能在同一个线程上发生:

static void Main(string[] args)
{
    Console.WriteLine("Starting on {0}", Thread.CurrentThread.ManagedThreadId);

    for (var i = 0; i < 10; i++)
    {
        var num = i;
        Task<int>.Factory.StartNew(() =>
        {
           if (num == 3)
           {
               Thread.Sleep(20000);
           }
           Thread.Sleep(new Random(num).Next(1000, 10000));
           Console.WriteLine("Done {0} on {1}", num, Thread.CurrentThread.ManagedThreadId);
           return num;
       }).ContinueWith(value =>
       {
           Console.WriteLine("Completed {0} on {1}", value.Result, Thread.CurrentThread.ManagedThreadId);
       } 

     /* need syncronization context */);
    }

    Console.WriteLine("End of Main");
    Console.ReadKey();
}

- 解决方案 - 在得到一些评论并阅读一些解决方案之后,这就是我想要的完整解决方案。这里的目标是尽可能快地处理长时间运行的任务,然后逐个处理每个任务的结果。

static void Main(string[] args)
{
    Console.WriteLine("Starting on {0}", Thread.CurrentThread.ManagedThreadId);

    var results = new BlockingCollection<int>();

    Task.Factory.StartNew(() =>
    {
        while (!results.IsCompleted)
        {
            try
            {
                var x = results.Take();
                Console.WriteLine("Completed {0} on {1}", x, Thread.CurrentThread.ManagedThreadId);
            }
            catch (InvalidOperationException)
            {
            }
        }
        Console.WriteLine("\r\nNo more items to take.");
    });

    var tasks = new List<Task>();

    for (var i = 0; i < 10; i++)
    {
        var num = i;
        var t = Task.Factory.StartNew(() =>
        {
            if (num == 3)
            {
                Thread.Sleep(20000);
            }
            Thread.Sleep(new Random(num).Next(1000, 10000));
            Console.WriteLine("Done {0} on {1}", num, Thread.CurrentThread.ManagedThreadId);
            results.Add(num);
        });

        tasks.Add(t);
    }

    Task.Factory.ContinueWhenAll(tasks.ToArray(), _ => results.CompleteAdding());

    Console.WriteLine("End of Main");
    Console.ReadKey();
}

4 个答案:

答案 0 :(得分:1)

您必须创建某种类型的编写器任务,但请记住,即使任务可以重新安排到另一个本机或托管线程上!使用TPL中的默认调度程序,您无法控制哪个托管线程接收工作。

public class ConcurrentConsole
{
    private static BlockingCollection<string> output
        = new BlockingCollection<string>();

    public static Task CreateWriterTask(CancellationToken token)
    {
        return new Task(
            () =>
            {
                while (!token.IsCancellationRequested)
                {
                    string nextLine = output.Take(token);
                    Console.WriteLine(nextLine);
                }
            },
            token);
    }

    public static void WriteLine(Func<string> writeLine)
    {
        output.Add(writeLine());
    }
}

当我切换你的代码使用它时,我收到了以下输出:

End of Main
Done 1 on 6
Completed 1 on 6
Done 5 on 9
Completed 5 on 9
Done 0 on 4
Completed 0 on 4
Done 2 on 5
Completed 2 on 13
Done 7 on 10
Completed 7 on 10
Done 4 on 8
Completed 4 on 5
Done 9 on 12
Completed 9 on 9
Done 6 on 6
Completed 6 on 5
Done 8 on 11
Completed 8 on 4
Done 3 on 7
Completed 3 on 7

即使您的代码向() => String.Format("Completed {0} on {1}"...发送ConcurrentConsole.WriteLine,确保在ManagedThreadId任务中选择ConcurrentConsole,它仍会改变其运行的线程。虽然可变性比执行任务少。

答案 1 :(得分:1)

您可以使用OrderedTaskScheduler确保一次只运行一个任务;但是,它们将在线程池线程上运行(不一定都在同一个线程上)。

如果你真的需要在同一个帖子上(不是一次只有一个),那么你可以使用Nito.Async library中的ActionThread。它为其代码提供了SynchronizationContext,可以通过FromCurrentSynchronizationContext获取。

答案 2 :(得分:0)

我建议:

1)创建锁定对象
 2)创建要写入的字符串列表
 3)生成一个循环,稍微休眠的线程,然后锁定字符串列表,然后如果它不为空,则写入所有这些并清空列表
 4)然后其他线程锁定列表,添加其状态,解锁并继续。

object writeListLocker = new object();
List<string> linesToWrite = new List<string>();

// Main thread loop
for (; ; )
{
    lock (writerListLocker)
    {
        foreach (string nextLine in linesToWrite)
            Console.WriteLine(nextLine);
        linesToWrite.Clear();
    }
    Thread.Sleep(500);
}

// Reporting threads
lock (writerListLocker)
{
    linesToWrite.Add("Completed (etc.)");
}

答案 3 :(得分:0)

我认为您期望得到如下结果。

Starting on 8
Done 1 on 11
Completed 1 on 9
Done 5 on 11
Completed 5 on 9
Done 0 on 10
Completed 0 on 9
Done 2 on 12
Completed 2 on 9
Done 7 on 16
Completed 7 on 9
Done 4 on 14
Completed 4 on 9
Done 9 on 18
Completed 9 on 9
Done 6 on 15
Completed 6 on 9
Done 8 on 17
Completed 8 on 9
Done 3 on 13
Completed 3 on 9

如下所示,我在the Understanding SynchronizationContext的代码中使用了StaSynchronizationContext,其中解释了一个线程中的同步调用。请参考它。

我的代码段是:

static void Main(string[] args)
{
    StaSynchronizationContext context = new StaSynchronizationContext();
    StaSynchronizationContext.SetSynchronizationContext(context);
    Console.WriteLine("Starting on {0}", Thread.CurrentThread.ManagedThreadId);
    for (var i = 0; i < 10; i++)
    {
        var num = i;
        Task<int>.Factory.StartNew(() =>
        {
            if (num == 3)
            {
                Thread.Sleep(20000);
            }
            Thread.Sleep(new Random(num).Next(1000, 10000));
            Console.WriteLine("Done {0} on {1}", num, Thread.CurrentThread.ManagedThreadId);
            return num;
        }).ContinueWith(
        value =>
        {
            Console.WriteLine("Completed {0} on {1}", value.Result, Thread.CurrentThread.ManagedThreadId);
        }
       ,TaskScheduler.FromCurrentSynchronizationContext());
    }
    Console.WriteLine("End of Main");
    Console.ReadKey();
}