I am using callbacks with the SynchronizationContext
object to update client UIs from a WCF service.
The code pattern I use is the following:
public void SetResults(string callId, IEnumerable<Result> results)
{
uiSyncContext.Post(new SendOrPostCallback(state => {
var r = (IEnumerable<Result>)state;
chart1.Series[callId].Points.DataBindXY(r.Select...
}, results);
}
The Post
method accepts a delegate, and a state object. There is no other signature.
Because I am using a anonymous method, it happens I tend to write:
public void SetResults(string callId, IEnumerable<Result> results)
{
uiSyncContext.Post(new SendOrPostCallback(state => {
chart1.Series[callId].Points.DataBindXY(results.Select...
}, null);
}
Because:
- It is shorter
- It spares a cast
- no needs to declare a local variable
Although it works, I am wondering what are the risks that the second approach could involve.
Is this considered as safe? Could the result parameter be "corrupted" somehow by subsequent calls?