Open wuyang26 opened 4 years ago
上面粘贴可能有点问题:
`public Task<List
// Wrap the WriteAsync logic in a TaskCompletionSource, so we can use C# async/await syntax to call it:
var taskCompletionSource = new TaskCompletionSource<List<bool>>();
m_session.BeginWrite(
requestHeader: null,
nodesToWrite: valuesToWrite,
callback: ar =>
{
var response = m_session.EndWrite(
result: ar,
results: out StatusCodeCollection results,
diagnosticInfos: out DiagnosticInfoCollection diag);
try
{
ClientBase.ValidateResponse(results, valuesToWrite);
ClientBase.ValidateDiagnosticInfos(diag, valuesToWrite);
List<bool> retList = new List<bool>();
foreach(var item in results)
{
retList.Add(StatusCode.IsGood(item));
}
taskCompletionSource.SetResult(retList);
}
catch (Exception ex)
{
taskCompletionSource.TrySetException(ex);
}
},
asyncState: null);
return taskCompletionSource.Task;
}`
没办法:
Task<List>应该是Task<List<bool>>
嗯,是个好的方式。
基于网络的数据读写,异步操作是非常重要的,这里依照项目中的其他代码修改了一个类似的: public Task<List> WriteNodeAsync(string[] tags, object[] values)
{
WriteValueCollection valuesToWrite = new WriteValueCollection();
for (int i = 0; i < tags.Length; i++)
{
if (i < values.Length)
{
WriteValue valueToWrite = new WriteValue()
{
NodeId = new NodeId(tags[i]),
AttributeId = Attributes.Value
};
valueToWrite.Value.Value = values[i];
valueToWrite.Value.StatusCode = StatusCodes.Good;
valueToWrite.Value.ServerTimestamp = DateTime.MinValue;
valueToWrite.Value.SourceTimestamp = DateTime.MinValue;
valuesToWrite.Add(valueToWrite);
}
}