Async await how to use return values

后端 未结 4 2025
猫巷女王i
猫巷女王i 2021-02-04 00:41

I have a windows service that I have inherited from another developer, it runs very slow and has numerous slow call to the eBay API. I wish to speed it up without too much refac

4条回答
  •  温柔的废话
    2021-02-04 01:07

    Try this code:

    public async Task ProcessAdditionalProductDetialsAsync(ItemType oItem) { 
        String additionalProductDetails = await Task.Run(() => {
           if (oItem.ItemSpecifics.Count > 0) {
              foreach (NameValueListType nvl in oItem.ItemSpecifics) { 
                 if (nvl.Value.Count > 0) {
                     string retval = String.Empty;
    
                     foreach (string s in nvl.Value) {
                         retval += "
  • " + nvl.Name + ": " + s + "
  • "; } } } } return retval; } return additionalProductDetails; }

    Usage:

    private async void GetAdditionalProductDetailsAsync(Action callback) {
       string apd = await ProcessAdditionalProductDetialsAsync();
       callback(apd);
    }
    
    private void AdditionalProductDetailsRetrieved(string apd) {
        // do anything with apd
    }
    

提交回复
热议问题