Task 类的实例(Task.Factory.StartNew 或 TaskCompletionSource)

发布于 2024-11-01 13:05:28 字数 538 浏览 0 评论 0原文

这可能是一个非常基本的问题,但只是我想确保我头脑中正确的问题。 今天我在挖掘 TPL 库,发现有两种创建 Task 类实例的方法。

方式一

 Task<int> t1 = Task.Factory.StartNew(() =>
                {
                    //Some code
                    return 100;

                });

方式二

  TaskCompletionSource<int> task = new TaskCompletionSource<int>();
  Task t2 = task.Task;
  task.SetResult(100);

现在,我只是想知道

  1. 这些实例之间有什么区别吗?
  2. 如果是的话怎么办?

This is probably a pretty basic question, but just something that I wanted to make sure I had right in my head.
Today I was digging with TPL library and found that there are two way of creating instance of Task class.

Way I

 Task<int> t1 = Task.Factory.StartNew(() =>
                {
                    //Some code
                    return 100;

                });

Way II

  TaskCompletionSource<int> task = new TaskCompletionSource<int>();
  Task t2 = task.Task;
  task.SetResult(100);

Now,I just wanted to know that

  1. Is there any difference between these instances?
  2. If yes then what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可遇━不可求 2024-11-08 13:05:45

由于您没有在上面的方式 1 中触发任何异步操作,因此您通过消耗线程池中的另一个线程来浪费时间(如果您不更改默认的 TaskScheduler )。

然而,在方式 2 中,您正在生成一个已完成的任务,并在您所在的同一个线程中执行该任务。 TCS 也可以被视为无线程任务(可能是错误的描述,但已被多个开发人员使用)。

As you are not firing any async operation in Way 1 above, you are wasting time by consuming another thread from the threadpool (possibly, if you don't change the default TaskScheduler).

However, in the Way 2, you are generating a completed task and you do it in the same thread that you are one. TCS can been also seen as a threadless task (probably the wrong description but used by several devs).

逆光下的微笑 2024-11-08 13:05:42

第二个示例没有创建“真正的”任务,即没有执行任何操作的委托。

您主要使用它来向调用者呈现任务界面。看一下上面的例子
msdn

    TaskCompletionSource<int> tcs1 = new TaskCompletionSource<int>();
    Task<int> t1 = tcs1.Task;

    // Start a background task that will complete tcs1.Task
    Task.Factory.StartNew(() =>
    {
        Thread.Sleep(1000);
        tcs1.SetResult(15);
    });

    // The attempt to get the result of t1 blocks the current thread until the completion source gets signaled.
    // It should be a wait of ~1000 ms.
    Stopwatch sw = Stopwatch.StartNew();
    int result = t1.Result;
    sw.Stop();

    Console.WriteLine("(ElapsedTime={0}): t1.Result={1} (expected 15) ", sw.ElapsedMilliseconds, result);

The second example does not create a "real" task, i.e. there is no delegate that does anything.

You use it mostly to present a Task interface to the caller. Look at the example on
msdn

    TaskCompletionSource<int> tcs1 = new TaskCompletionSource<int>();
    Task<int> t1 = tcs1.Task;

    // Start a background task that will complete tcs1.Task
    Task.Factory.StartNew(() =>
    {
        Thread.Sleep(1000);
        tcs1.SetResult(15);
    });

    // The attempt to get the result of t1 blocks the current thread until the completion source gets signaled.
    // It should be a wait of ~1000 ms.
    Stopwatch sw = Stopwatch.StartNew();
    int result = t1.Result;
    sw.Stop();

    Console.WriteLine("(ElapsedTime={0}): t1.Result={1} (expected 15) ", sw.ElapsedMilliseconds, result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文