public async Task WaitForAsync(string host, string serverName, string tagID, short waitFor, ILog logger) { EasyDAClient opcComponent = new EasyDAClient(); opcComponent.Isolated = true; opcComponent.InstanceParameters.Mode.AllowAsynchronousMethod = false; var result = await TryWaitForAsync(opcComponent, host, serverName, tagID, waitFor, logger); opcComponent.UnsubscribeAllItems(); return 1; } private Task TryWaitForAsync(EasyDAClient opcComponent, string host, string serverName, string tagID, T waitFor, ILog logger) where T:struct { TaskCompletionSource tcs = new TaskCompletionSource(); opcComponent.ItemChanged += (s, e) => { try { if (e.Vtq != null) { if (e.Vtq.Quality.IsGood()) { string itemId = e.ItemDescriptor.ItemId; if (e.Vtq.Value is T && waitFor.Equals(e.Vtq.Value)) { tcs.SetResult((T)e.Vtq.Value); } else if (e.Vtq.Value is Int16 || (Int16)e.Vtq.Value > 90) { if ((Int16)e.Vtq.Value == (Int16)MES.Constants.RESULTCODE.RECIPE_ERROR) { tcs.SetException(new MES.Common.Exceptions.PlcErrorCodeException(MES.Constants.RESULTCODE.RECIPE_ERROR, "Unknown recipe")); } if ((Int16)e.Vtq.Value == (Int16)MES.Constants.RESULTCODE.OUT_OF_MATERIAL) { tcs.SetException(new MES.Common.Exceptions.PlcErrorCodeException(MES.Constants.RESULTCODE.OUT_OF_MATERIAL, "Out of material")); } } } else { logger.DebugFormat("Quality WaitForAsync : {0}", e.Vtq.Quality.StatusBitField); } } } catch(Exception ex) { logger.Fatal("OPCReader.WaitForAsync", ex); throw; } }; opcComponent.SubscribeItem(host, serverName, tagID, 300); return tcs.Task; }