等待异步方法在c#调用

问题描述:

我有一个WCF服务如下等待异步方法在c#调用

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

方法twcPMRefresh()和twcCommonRefresh(内部的逻辑)是异步的,因此我想等到“twcPMRefresh” exceution是执行完毕 ,然后调用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 
} 

请帮助。谢谢!!

+0

你可以使用TPL并使用'Task.ContinueWith'调用。 – Tdorno

+0

在RefreshDB()方法中建议任务continueWith?我尝试了下面的方式,但仍然没有运气。语法中有什么错误吗? var firstTask = new Task(()=> twcPMRefresh()); var secondTask = firstTask.ContinueWith((t)=> twcCommonRefresh()); firstTask.Start(); –

+0

如果您显示两种方法的签名,那将会很棒。 – Enigmativity

可以使用await async方法:

假设你twcPMRefresh()twcCommonRefresh()方法都是异步你可以写这样的:

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.