我怎样才能在.NET 4.5中运行这两种方法“在同一时间”?这两种、时间、方法、在同一

2023-09-03 12:54:49 作者:丑的性感/

我有做2的独立的片逻辑的方法。我希望我可以运行他们两个的同时的..只有不断事后,当这两个孩子的方法完成。

I have a method which does 2 independent pieces of logic. I was hoping I can run them both at the same time .. and only continue afterwards when both those child methods have completed.

我试图让我的头周围的异步/计谋的语法,但我只是不明白这一点。

I was trying to get my head around the async/await syntax but I just don't get it.

这里的code:

public PewPew SomeMethod(Foo foo)
{
    var cats = GetAllTheCats(foo);
    var food = GetAllTheFood(foo);

    return new PewPew
               {
                   Cats = cats,
                   Food = food
               };
}

private IList<Cat> GetAllTheCats(Foo foo)
{
    // Do stuff, like hit the Db, spin around, dance, jump, etc...
    // It all takes some time.
    return cats;
}

private IList<Food> GetAllTheFood(Foo foo)
{
    // Do more stuff, like hit the Db, nom nom noms...
    // It all takes some time.
    return food;
}

所以与code以上,我想说的是:去把所有的猫和食品在同一时间。一旦我们完成了,然后返回一个新的 PewPew

我很困惑,因为我不知道哪个以上是类异步或返回工作等所有的EM?只是两个私立的?我也猜我需要利用 Task.WaitAll(任务)的方法,但我不能确定如何的设置的任务运行同时

I'm confused because I'm not sure which classes above are async or return a Task, etc. All of em? just the two private ones? I'm also guessing I need to leverage the Task.WaitAll(tasks) method, but I'm unsure how to setup the tasks to run at the same time.

建议,这类人?

推荐答案

下面是你可能想要做的:

Here is what you may want to do:

public async Task<PewPew> SomeMethod(Foo foo)
{
    // get the stuff on another thread 
    var cTask = Task.Run(() => GetAllTheCats(foo));
    var fTask = Task.Run(() => GetAllTheFood(foo));

    var cats = await cTask;
    var food = await fTask;

    return new PewPew
               {
                   Cats = cats,
                   Food = food
               };
}

public IList<Cat> GetAllTheCats(Foo foo)
{
    // Do stuff, like hit the Db, spin around, dance, jump, etc...
    // It all takes some time.
    return cats;
}

public IList<Food> GetAllTheFood(Foo foo)
{
    // Do more stuff, like hit the Db, nom nom noms...
    // It all takes some time.
    return food;
}

有两件事情你需要了解这里:

There are two things you need to understand here:

1)什么是差异这样的:

1) What is diff between this:

var cats = await cTask;
var food = await fTask;

和这样的:

Task.WaitAll(new [] {cTask, fTask});

这两个给你在这个意义上相似的结果让2异步任务完成,然后返回新PewPew - 然而,温差为这Task.WaitAll()将阻止当前线程(如果这是UI线程,那么用户界面将冻结)。相反,等待将打破的someMethod 说,在一个状态机,并从的someMethod 返回到它的调用者,因为它遇到等待的关键字。它不会阻塞线程。在code以下伺机将如期运行时异步任务已经结束了。

Both will give you similar result in the sense let the 2 async tasks finish and then return new PewPew - however, differnce is that Task.WaitAll() will block the current thread (if that is UI thread, then UI will freeze). instead, await will break down the SomeMethod say in a state machine, and return from the SomeMethod to its caller as it encounters await keyword. It will not block the thread. The Code below await will be scheduled to run when async task is over.

2)您也可以做到这一点:

2) You could also do this:

var cats = await Task.Run(() => GetAllTheCats(foo));
var food = await Task.Run(() => GetAllTheFood(foo));

然而,这将不同时开始异步任务。第二个任务将启动后的第一个结束。这是因为如何的await关键字的工作。 HTH。

However, this will not start the async tasks simultaneously. Second task will start after the first is over. This is because how the await keyword works. HTH.

编辑:如何使用的someMethod - 某处调用树的开始,你必须使用wait()的,或导致财产 - 或 - 你必须从等待异步无效。 一般情况下,异步空将是一个事件处理程序:

How to use SomeMethod - somewhere at the start of the call tree, you have to use Wait() or Result property - OR - you have to await from async void. Generally, async void would be an event handler:

public async void OnSomeEvent(object sender, EventArgs ez) 
{ 
  Foo f = GetFoo();
  PewPew p = await SomeMethod(f);
}

如果没有,那么使用结果属性。

If not then use result property.

public Foo2 NonAsyncNonVoidMethod() 
{
   Foo f = GetFoo();
   PewPew p = SomeMethod(f).Result; //But be aware that Result will block thread

   return GetFoo2(p);
}