简体   繁体   English

c#中的闭包示例中的async关键字

[英]async keyword in a closure example in c#

I am going thru this blog to understand how to increment load. 我将通过此博客来了解如何增加负载。

http://marcominerva.wordpress.com/2013/05/22/implementing-the-isupportincrementalloading-interface-in-a-window-store-app/ http://marcominerva.wordpress.com/2013/05/22/implementing-the-isupportincrementalloading-interface-in-a-window-store-app/

I need help in understanding the following method: I think: 我需要帮助来了解以下方法:我认为:

()  =>
{

}

mean a closure with no input argument. 表示没有输入参数的闭包。 Why it adds 'async' before () =>? 为什么在()=>之前添加'async'? Why the method LoadMoreItemsAsync() return IAsyncOperation<>, but it calls 'Task.Run'? 为什么方法LoadMoreItemsAsync()返回IAsyncOperation <>,但却调用了“ Task.Run”? Did the operantion ran before it returns? 操作是否在返回之前就运行了?

 public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
    {
        var dispatcher = Window.Current.Dispatcher;

        return Task.Run<LoadMoreItemsResult>(
            async () =>   // why aysnc here?
            {
                uint resultCount = 0;
                var result = await source.GetPagedItems(currentPage++, itemsPerPage);

                if (result == null || result.Count() == 0)
                {
                    hasMoreItems = false;
                }
                else
                {
                    resultCount = (uint)result.Count();

                    await dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        () =>
                        {
                            foreach (I item in result)
                                this.Add(item);
                        });
                }

                return new LoadMoreItemsResult() { Count = resultCount };

            }).AsAsyncOperation<LoadMoreItemsResult>();
    }

() => { /* ... */ } is a lambda without input parameter, in terms of a delegate it would be an Action . () => { /* ... */ }是不带输入参数的lambda,就委托而言,它将是Action

To use the await keyword the method has to be marked with async . 要使用await关键字,必须用async标记该方法。 Since the body of that lambda function contains await you have to use that async lambda syntax. 由于该lambda函数的主体包含await您必须使用该异步lambda语法。

The purpose of the LoadMoreItemsAsync method is to return almost immediately and continue loading items in the background, ie asynchronous with respect to the calling thread. LoadMoreItemsAsync方法的目的是几乎立即返回并继续在后台加载项目,即相对于调用线程而言是异步的。 THis is done because presumably the loading takes some time and you don't want to block the UI. 之所以这样做,是因为加载可能要花费一些时间,并且您不想阻塞UI。

In order to do so it uses the Task.Run method which returns a Task<LoadMoreItemsResult> and AsAsyncOperation<LoadMoreItemsResult>() turns that into an object which implements the IAsyncOperation<LoadMoreItemsResult> interface. 为此,它使用Task.Run方法返回Task<LoadMoreItemsResult>AsAsyncOperation<LoadMoreItemsResult>()将其转换为实现IAsyncOperation<LoadMoreItemsResult>接口的对象。

A Lambda Expression which runs runs async code needs to be marked async in order to use the await keyword inside it. 需要将运行async代码的Lambda Expression标记为async ,以便在其中使用await关键字。 Similar to the idea that a Named Method needs to mark itself async . 类似于Named Method需要将自身标记为async的想法。

This signals the compiler that a state machine should be generated in the execution flow. 这向编译器发出信号,表明应该在执行流程中生成状态机。

This: 这个:

var result = await source.GetPagedItems(currentPage++, itemsPerPage);

And this: 和这个:

await dispatcher.RunAsync(
           CoreDispatcherPriority.Normal,
           () =>
           {
               foreach (I item in result)
                   this.Add(item);
           });

Need the async modifier to run. 需要async修改器才能运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM