Threading and Appraisal example ( Is this right?)

Topics: CAB & Smart Client Software Factory
Mar 2, 2007 at 7:27 PM
In the AppraisalWorkbench sample's AppraisalManagementServiceAgent.cs file, there are several methods that use anonymous delegates in conjunction with Smart Web Services. The method GetAppraisals (line 171) for instance takes a filter and a callback as a parameter.

Within the anonymous delegate callback on line 186 these two fields are referenced from the smart web service CommandQueue thread via the callback. Note they are not passed into the delegate, but are referenced off of the call stack of the GetAppraisals method.

In c++, these fields would have been on the call stack, and by the time the delegate executed( in the context of another thread ), the stack would have been cleared ( the containing GetAppraisals method had returned ) and the fields filter and callback would probably have a different value in the locations referenced for those fields. Does C# know that those two values need to stay around with their present values? There is no reference to them in the delegates parms, just in its body. If a second call is made to AppraisalManagementServiceAgent.GetAppraisals before the first is complete, will the right values be used?

I am asking this here because the pattern is shown in the SCSF example. If this is not a thread safe pattern to use, it would probably be good to know. If it is, I would like to understand the underlying concepts and assumptions. i.e. how does C# get the proper value for filter and callback.

Mar 5, 2007 at 12:57 PM
I knew someone had to have been through this before.

Thanks Ian.