【问题标题】:How to async task and Mailchimp API v3.0如何异步任务和 Mailchimp API v3.0
【发布时间】:2017-07-07 09:17:16
【问题描述】:

我正在尝试从 MailChimp 获取 MailChimp 列表集合。我已经按照 MailChimp.net 上的示例设置了获取所有列表的过程,但它在返回列表之前退出,除非我在调用任务后使用控制台读取。我如何才能让这个看似简单的任务发挥作用?

static void Main(string[] args)

{

    AddUpdateMailChimp();

    Console.Read();

}

static async void AddUpdateMailChimp()

{

    lstIDs = await Get_MailChimp_Info();

    for(int i = 0; i < lstIDs.Count; i++)

    {

        AddUpDateMailChimpAsync(lstIDs[i]);
    }

}

private static async Task< List< string >> Get_MailChimp_Info()

{

    var lstIDs  = new List< string > ();

    apikey = GetApiKey() //from config file

    manager = new MailChimpManager(apikey);
    //............below line is where it bombs unless I use a concole.Read in the main..........//

   **IEnumerable< MailChimp.Net.Models.List> mailChimpListCollection = await manager.Lists.GetAllAsyunc().ConfigureAwait( continueOnCapturedContext: false);**

    ............catch statements

    //.......foreach loop to get the list Ids
}

【问题讨论】:

    标签: c# automation console-application mailchimp-api-v3.0


    【解决方案1】:

    我最近遇到了这个问题。因为AddUpdateMailChimp() 是异步的,所以Main() 方法一碰到它就会继续执行,而不是等待结果。

    该方法还返回 void,因此它被视为一劳永逸。

    如果您希望能够等待它,那么它应该返回一个任务。

    这样,在Main()你就可以做到

    var result = AddUpdateMailChimp().Result;

    【讨论】:

    • 谢谢,我会试一试的。
    猜你喜欢
    • 2016-05-02
    • 2016-01-11
    • 2017-06-11
    • 2015-08-02
    • 2016-05-03
    • 2016-07-03
    • 2016-11-24
    • 2015-12-20
    • 2016-02-17
    相关资源
    最近更新 更多