对于以下代码我调用async void方法.
public void LoadSystemDetails(int clientId)
{
DataSystem.Name = "Salesforce System";
DataSystem.Description = "";
GetAllFields(clientId);
}
Run Code Online (Sandbox Code Playgroud)
以下代码是GetAllFields方法
public async void GetAllFields(int clientId)
{
this.DataSystem.SystemTables = new List<DataSystemTable>();
using (var forceClient = await ConnectToSalesforceByOAuth(clientId))
{
var SalesForceTable = new DataSystemTable
{
TableName = "Contact"
};
DataSystem.SystemTables.Add(SalesForceTable);
var contact = forceClient.DescribeAsync<SalesforceObject>("Contact");
var tableFields = new List<DataSystemField>();
foreach (var con in contact.Result.Fields)
{
tableFields.Add(new DataSystemField
{
ColumnName = con.Name,
});
}
SalesForceTable.EissSyncSystemFields = tableFields;
}
Run Code Online (Sandbox Code Playgroud)
我打电话callbackScript如下.
callbackScript.AppendLine(string.Format("var destinationSystem ={0};", JsonConvert.SerializeObject(this.DestinationSystem, Formatting.Indented)));
Run Code Online (Sandbox Code Playgroud)
这里DestinationSystem是打电话给LoadSystemDetails.喜欢DestinationSystem.LoadSystemDetails(clientId)
而using (var forceClient = await ConnectToSalesforceByOAuth(clientId))行在执行时callbackScript 执行.所以SystemTables没有任何价值.但它有Name和Description.
所以我需要等待LoadSystemDetails完成GetAllFields.
我是怎么做的.请帮帮我.谢谢.
如果你需要LoadSystemDetails等待GetAllFields,这里有两个问题:
GetAllFields是异步无效,这意味着火与忘记.你将永远无法等待它完成.解决方案:首先,async void如果您需要等待结果或结束,请勿使用.请async Task改用
二,转换LoadSystemDetails为异步方法,然后等待GetAllFields(应该返回Task),或者使用GetAllFields(clientId).Wait()
有关async/await的更多信息,请查看本文:https://msdn.microsoft.com/en-us/magazine/jj991977.aspx