如何强制执行停止,直到异步函数完全执行? [英] How to force execution to stop till asynchronous function is fully executed?

查看:241
本文介绍了如何强制执行停止,直到异步函数完全执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建CRM Silverlight应用程序如下:

I'm creating a silverlight application for CRM as follow:

1- 用户控件这是一个形式充满了使用从CRM中检索的数据异步/的await

1- A usercontrol which is a form is filled with data retrieved from the CRM using async/await

2- 打印按钮创建的用户控件和印刷品的一个实例是

2- A Print button that creates an instance of that usercontrol and prints it

我有一个问题的执行序列的导致打印按钮没有数据打印的用户控件,这意味着,它的异步方法完成之前执行。

I have a problem in the sequence of execution that causes the Print button to print the usercontrol with no data, meaning, it's executed before the async method finishes.

我的code是如下:

用户控制:

   Public partial class ManagerContact : UserControl
     // constructor and functions
     ... 
   async private  void getData(string contactid)
    {
        // get some details of the contact 

            QueryExpression query = new QueryExpression();
            query.EntityName = "contact";
            ColumnSet cset = new ColumnSet();
            cset.Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new String[] { "emailaddress1","fullname"  });  
            query.ColumnSet = cset;


            IOrganizationService service = SilverlightUtility.GetSoapService();
            var response = await service.RetrieveMultiple(query);

           /// PROBLEM HERE

            List<contacts> mycontactlist= new List<contacts>();
           // a function to fill the controls in the form with the retrieved data
            ...
         }
     }

我的打印按钮:

   private  void PrintOrExport(object sender, RoutedEventArgs e)
    {
        PrintDocument document = new PrintDocument();

       ManagerContact mymanager= new Common.ManagerContact(mycontactid);

             document.PrintPage += (s, args) =>
            {
              // code here to prepare the pagevisual
            }
       document.Print("title");
     } 

当我点击打印,将创建ManagerContact用户控件的新实例和执行经过用户控件的所有功能,直到它到达行

When I click Print, a new instance of ManagerContact user control is created and execution goes through all the functions in the user control till it reaches the line

   var response = await service.RetrieveMultiple(query);

和然后它返回到打印按钮的功能,并进入打印网页。问题是,它跳过的,我填写表单控件与数据等形式印有没有数据功能的其余部分。

and then it returns back to the Print Button function and proceeds to print the page. The problem is it skips the rest of the functions where I fill the form controls with the data and so the form is printed with no data.

我需要找到一种方法来强制执行,以等待要返回的响应,只有它得到响应德执行会回到主页打印按钮后剩下的。

I need to find a way to force execution to wait for the response to be returned and only after it gets teh response execution would go back to the main page for the rest of the print button.

推荐答案

您的问题是由于异步无效。作为一般原则,避免异步无效;它应该只用于事件处理程序。欲了解更多信息,请参阅我的异步最佳实践的MSDN文章。

Your problem is due to async void. As a general guideline, avoid async void; it should only be used for event handlers. For more information, see my MSDN article on asynchronous best practices.

所以,你的的getData 方法应该是这样的:

So, your getData method should look like this:

private async Task getDataAsync(string contactid)

和任何调用它应该等待工作返回,这意味着的的功能也需要异步并返回任务任务&LT; T&GT;

And whatever calls it should await the Task it returns, which means that function also needs to be async and return Task or Task<T>, etc.

最后,你会发现,你的构造不能异步,所以我建议你创建一个异步工厂方法的为我描述我的博客

Eventually, you'll find that your constructor cannot be async, so I recommend you create an asynchronous factory method as I describe on my blog:

class ManagerContact
{
  private ManagerContact(int contactId)
  {
    ...
  }

  private async Task InitializeAsync()
  {
    await getDataAsync();
    ...
  }

  public static async Task<ManagerContact> CreateAsync(int id)
  {
    var result = new ManagerContact(id);
    await result.InitializeAsync();
    return result;
  }
}

然后你可以使用这样的:

Which you can then use like this:

private async void PrintOrExport(object sender, RoutedEventArgs e)
{
  ManagerContact mymanager = await Common.ManagerContact.CreateAsync(mycontactid);
  ...
}

这篇关于如何强制执行停止,直到异步函数完全执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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