簡體   English   中英

等待C#中的Async方法調用

[英]Wait for Async method calls in c#

我有以下WCF服務

public bool RefreshDB()
{
   if (twcPMRefresh())
            status = true;
   if (twcCommonRefresh())
            status = true;
}

twcPMRefresh()和twcCommonRefresh()方法內部的邏輯是異步的,因此我想等到“ twcPMRefresh”執行完成后再調用twcCommonRefresh。 兩種方法都比較少,如下所示。

public bool twcPMRefresh()
{
     tweets=await
         (from tweet in GetTwitterContext(localProxyIP,fCode).Status
         where tweet.Type == StatusType.User &&
         select tweet)
     .ToListAsync();
     --use 'tweets' list to insert in database
 }

可以請協助。 謝謝!!

您可以使用await async方法:

假設您的twcPMRefresh()twcCommonRefresh()方法都是ASYNC,則可以這樣編寫:

public async bool RefreshDB()
{
   if (await twcPMRefresh())
       status = true;
   if (await twcCommonRefresh())
       status = true;
}

您的方法RefreshDB()現在可以通過以下兩種方式調用:

bool result = RefreshDB().Result()
bool result = await RefreshDB(); //Needs an async Method or a task to be used.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM