RemoteVariableSynchronizer() 构造函数
创建
RemoteVariableSynchronizer
对象,该对象包含要与相关字段变量保持同步的项目变量的列表。public RemoteVariableSynchronizer()
重要提示:
RemoteVariableSynchronizer
对象必须声明为 NetLogic 中的类成员。具有默认轮询时间的构造函数
在以下示例中,创建一个
variableSynchronizer
对象,该对象使 Speed
项目变量的值与用于标识电机速度的字段变量的值保持同步。通过 Add()
方法将 motorSpeed
对象表示的项目变量添加到 variableSynchronizer
对象中。当字段变量的值发生变化时,将执行
motorSpeed_VariableChange
方法。当(字段变量的)motorSpeed
值大于 200 时,将生成警告消息。默认轮询时间设置为全速。private void Start() { motorSpeed = LogicObject.Owner.GetVariable("Speed");variableSynchronizer = new RemoteVariableSynchronizer();variableSynchronizer.Add(motorSpeed); motorSpeed.VariableChange += MotorSpeed_VariableChange; } private void MotorSpeed_VariableChange(object sender, VariableChangeEventArgs e) { if (motorSpeed.Value > 200) { Log.Warning("Speed limit reached!"); } } private IUAVariable motorSpeed; private RemoteVariableSynchronizer variableSynchronizer;
在
Stop()
方法中,当移除 NetLogic 父节点时,将通过调用 Dispose()
方法来结束同步。具有自定义轮询时间的构造函数
在以下示例中,
RemoteVariableSynchronizer
的轮询时间设置为 5000
毫秒。public override void Start() { var variable1 = Project.Current.Get<IUAVariable>("Model/Variable1"); variable1.VariableChange += Variable1_VariableChange;synchronizer = new RemoteVariableSynchronizer(TimeSpan.FromMilliseconds(5000));synchronizer.Add(variable1); } private void Variable1_VariableChange(object sender, VariableChangeEventArgs e) { Log.Info($"Variable1 {e.NewValue}"); } public override void Stop() { synchronizer.Dispose(); var variable1 = Project.Current.Get<IUAVariable>("Model/Variable1"); variable1.VariableChange -= Variable1_VariableChange; } RemoteVariableSynchronizer synchronizer;
提供反馈