C# 我必须做什么才能使我的方法可等待?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13518619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:52:31  来源:igfitidea点击:

What must I do to make my methods awaitable?

c#windows-8async-awaitwindows-store-apps

提问by B. Clay Shannon

How can I roll my own async awaitable methods?

如何推出自己的异步等待方法?

I see that writing an async method is easy as pie in some cases:

我发现在某些情况下编写异步方法很容易:

private async Task TestGeo()
{
    Geolocator geo = new Geolocator();
    Geoposition pos = await geo.GetGeopositionAsync();
    double dLat = pos.Coordinate.Latitude;
    double dLong = pos.Coordinate.Latitude;
}

...but sadly also see that not just any method can be made async willy-nilly, though; to wit: this doesn't work:

...但遗憾的是,也不是任何方法都可以随意异步;即:这不起作用:

private async Task TestAsyncAwait()
{
    int i = await TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5);
}

...it stops me with the compile error, "Cannot await int"; a hint at design time similarly tells me, "type 'int' is not awaitable"

...它阻止我编译错误,“不能等待 int”;设计时的提示同样告诉我,“类型 'int' 不可等待”

I also tried this with the same results:

我也尝试过,结果相同:

    Task<int> i = await TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5);

What must I do to make my methods awaitable?

我必须做什么才能使我的方法可等待?

UPDATE

更新

As Linebacker and S. Cleary indicated (any relation to that cat who used to be on KNBR?), this works:

正如 Linebacker 和 S. Cleary 指出的(与曾经在 KNBR 上的那只猫有任何关系?),这是有效的:

int i = await Task.Run(() => TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5));

...that is to say, it compiles-- but it never "moves."

...也就是说,它会编译——但它永远不会“移动”。

At runtime, it tells me I should "await" the CALL to TestAsyncAwait(), but if I do that, it doesn't compile at all...

在运行时,它告诉我应该“等待”对 TestAsyncAwait() 的 CALL,但如果我这样做,它根本不会编译......

采纳答案by Stephen Cleary

You only needto return an awaitable. Task/Task<TResult>is a common choice; Tasks can be created using Task.Run(to execute code on a background thread) or TaskCompletionSource<T>(to wrap an asynchronous event).

你只需要返回一个 awaitable。Task/Task<TResult>是一个常见的选择;Tasks 可以使用Task.Run(在后台线程上执行代码)或TaskCompletionSource<T>(包装异步事件)来创建。

Read the Task-Based Asynchronous Patternfor more information.

阅读基于任务的异步模式了解更多信息。

回答by Mayank

You need to either consume the awaited return or return Task<Type of await call>

您需要消耗等待的返回或返回 Task<Type of await call>

  1. private async Task<Geoposition> TestGeo()
    {
        Geolocator geo = new Geolocator();
        return await geo.GetGeopositionAsync();
    }
    
  2. private async Task<int> TestAsyncAwait()
    {
        return await TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5);
    }
    
  1. private async Task<Geoposition> TestGeo()
    {
        Geolocator geo = new Geolocator();
        return await geo.GetGeopositionAsync();
    }
    
  2. private async Task<int> TestAsyncAwait()
    {
        return await TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5);
    }
    

回答by p3n

var something = Task<int>.Factory.StartNew(() => 0);
something.Wait();
int number = something.Result;

回答by Jeson Martajaya

Your method

你的方法

private async Task TestAsyncAwait()
{
    int i = await TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5);
}

should be written like this

应该这样写

private async Task TestAsyncAwait()
{
    Task<int> t = new Task<int>(() =>
    {
        return TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5);
    });
    t.Start();
    await t;
}

If you need to return the int, replace the Task type:

如果需要返回 int,请替换 Task 类型:

private async Task<int> TestAsyncAwait()
{
    Task<int> t = new Task<int>(() =>
    {
        return TaSLs_Classes.TASLsUtils.GetZoomSettingForDistance(5);
    });
    t.Start();
    return await t;
}

More explanation here.

更多解释在这里