如何在后台运行非必要的计算? [英] How do I make non-essential computations run in the background?

查看:29
本文介绍了如何在后台运行非必要的计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是在跟进 为什么是此代码同步运行? .我意识到我真正的问题比那篇文章中的问题要高.我现在在下面问的问题是我如何完成这项工作?"

This question is in follow up to Why is this code running synchronously? . I realize that my real question is at a higher level than the one in that post. The question I now ask, below, is "how do I accomplish this?"

我想在 C# 中使用并发来在后台计算事物.我有 ptsTin 类,它代表一个现有的地面.我想让加载尽可能快.有些工作是必不可少的,因为在工作完成之前您没有实例.例如,在 .LoadPoints() 和 .LoadTriangles 都完成之前,没有 ptsTin 实例.

I want to use concurrency in C# to compute things in the background. I have class ptsTin, which represents an existing ground surface. I want to make loading as fast as possible. Some of the work is essential in that you don't have an instance until the work is complete. For example, there is no ptsTin instance until .LoadPoints() and .LoadTriangles are both complete.

工作的其他部分不是必需的,可以稍后计算,即使稍后是 0.2 秒.我想在一个新线程中开始非必要的工作并忘记它.如果它正在处理的值尚未准备好,则该值将为空.

Other parts of the work are not essential and may be computed later, even if later is 0.2 seconds later. I would like to start the non-essential work in a new thread and forget about it. If the value that it is working on is not ready yet, the value will just be null.

所以这就是我想要做的.代码现在在控制台应用程序中,但有一天会在 GUI 应用程序中.请注意,这是伪代码.我知道它不会像这样工作,它的目的是传达我想知道如何做的事情:

So here is what I would like to do. The code is now in a console app, but one day will be in a GUI app. Note, this is pseudo code. I know it will not work like this, and its purpose is to communicate what I want to know how to do:

  static void Main(string[] args)
  {
      var myTin = ptsDTM.Load("SomeFile.ptsTin");
      // Do other stuff here like getElevationAtAPoint();
  }

  public static ptsTin Load(String file)
  {
     // Essential Work
     ptsTin newTin = new ptsTin();
     newTin.LoadPoints(file);
     newTin.LoadTriangles(file);

     // At this point I could return, but I want other stuff done
     // Non-essential work -- I want this on a "background" task
     newTin.BoundingBox = newTin.ComputeBBInTheBackground();    

     // Now return without waiting for ComputeBB to finish
     return newTin;
  }

如果另一个方法稍后要求 tin.BoundingBox,但该值尚未准备好,则它仍为 null.其他方法会知道该值不为空时有效.

If another method later asks for tin.BoundingBox, but the value is not ready yet, it is still null. Other methods will know that the value is valid when it is not null.

我该如何实现?

我对答案是使用 async 和 await、Task.Run 或任何其他方法没有偏好.

I have no preference whether or not the answer is to use async and await, Task.Run, or any other approach.

推荐答案

我该如何实现?

您可以通过将 BoundingBox 更改为将在将来完成的 Task 来实现.

You can do this by changing BoundingBox to a Task<BoundingBox> which will be completed in the future.

public static ptsTin Load(String file)
{
    // Essential Work
    ptsTin newTin = new ptsTin();
    newTin.LoadPoints(file);
    newTin.LoadTriangles(file);

    // At this point I could return, but I want other stuff done
    // Non-essential work -- I want this on a "background" task
    newTin.BoundingBox = Task.Run(() => newTin.ComputeBB());

    // Now return without waiting for ComputeBB to finish
    return newTin;
}

现在,您可以在其他方法中查找状态:

Now, you can look up the status in other methods:

if (ptsTin.BoundingBox.Status == TaskStatus.Completed)
{
     // Finished computing
}

这篇关于如何在后台运行非必要的计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆