when i read the source code of csla, i have some questions.pls help to answer.
i have a property (named filedA) with a async rule(business rule not validation rule),it calls the server to calculate something and then update another property (named filedB) on the completed callback thread.
now , i have another thread directly update the fieldB, both the two thread will update the filedB by SetFieldData<P>(IPropertyInfo prop, P value) method with no sync lock ?
It is not thread safety, right?
And how can i make this scenario safety.
I may not fully understand yout scenario - however, this is the general rule:
First, I dont't bind this OBJ to UI, So there's no "UI Thread".
Second, I implement my async rule as below:
public class MyRule:BussinesRule
protected virtual void Execute(RuleContext context)
System.Threading.Thread myThread = new Thread(new ParameterizedThreadStart((ThreadRun));
myThread .IsBackground = true;
private void ThreadRun(object context)
RuleContext rulecontext = context as RuleContext context;
rulecontext.Completed(); //the completed isn't called on the original call thread.
well, that's not how you are supposed to create an async rule.
You will create a new thread for each time the rule is executed.
You should rather look at:
Backgroundworker must be used with the UI Thread,otherwise i must implement subclass of SynchronizationContext which maybe using "await".
DataProtal used the Backgroundworker with the UI Thread, too.
TreadPool also need using "await" to guarantee the thread safe.
So, we must keep in mind that if we dosn't used the UI Thread, we must do something to guarantee the thread safe. Right?
BackgroundWorker can be used in different types of applications (including Console apps). And .NET has (IIRC) 4 different implementations of the SynchronizationContext to support different types of apps (without the use of async).
So you can use DataPortal or BackgroundWorker in any type of application.
With thread pool you can specify a callback method to be executed on the "original" thread after the async operation has completed. You are NOT forced to use async/await with the thread pool
You could also create Task or Task<T> objects and use async/await pattern.
I highly recommend the "Threading in C#" free ebook from Joe Albahari, available for online reading or download as PDF
There is also a very good "Async introduction" with sample available for download ("more samples") in LinqPad, from http://www.linqpad.net/
Asynchrony in C# 5 Interactive Tutorial
Copyright (c) Marimer LLC