Reputation: 1
In the example below, when the Tag1_changed and Tag2_changed methods are invoked, they require the form to be invoked to update a control or else an exception is thrown. However, when using the lambda method for the callback parameter, this.InvokeRequired evaluates to true but the control is still updated and no exception is thrown even though no MethodInvoker for the form is used. Why doesn't the (pnlHeartbeat.BackColor) statement throw an exception when executed?
private void BtnSubscribe_Click(object sender, EventArgs e)
{
var task = AppGlobals.opc.SubscribeToTagAsync("k_Tag1", Tag1_changed);
task = AppGlobals.opc.SubscribeToTagAsync("k_Tag2", Tag2_changed);
task = AppGlobals.opc.SubscribeToTagAsync("k_Heartbeat", (tagname, tag) =>
{
try
{
bool ir = this.InvokeRequired; //this is always true
pnlHeartbeat.BackColor = tag.Value.ToBooleanOrDefault(false) ? System.Drawing.Color.Green : System.Drawing.Color.Red; //value updates fine without exception
}
catch (Exception ex)
{
string exm = ex.Message; //never gets here even though InvokeRequired is true
}
});
}
private void Tag1_changed(string tagname, DataValue tag)
{
this.BeginInvoke(new MethodInvoker(() =>
{
lblTag1Value.Text = tag.Value.ToString();
}));
}
private void Tag2_changed(string tagname, DataValue tag)
{
this.BeginInvoke(new MethodInvoker(() =>
{
lblTag2Value.Text = tag.Value.ToString();
}));
}
SubscibeToTagAsync method signature is:
public async Task SubscribeToTagAsync(string tagname, Action<string, DataValue> callback)
The callback Action is stored away in an OnChange property and eventually executed in the following way:
this.Tags[tag.Key].OnChange?.Invoke(tag.Key, mon.Value);
Upvotes: 0
Views: 68