Is Thread safe When AsyncBusinessRule completed???

Is Thread safe When AsyncBusinessRule completed???

Old forum URL: forums.lhotka.net/forums/t/12164.aspx


Sam_Lin posted on Friday, September 27, 2013

Dear all:

         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.

                                                                                                                 Best Reagards!

JonnyBee replied on Friday, September 27, 2013

I may not fully understand yout scenario - however, this is the general rule:

 

Sam_Lin replied on Friday, September 27, 2013

Thanks Jonny.

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;

                             myThread .Start(context);

                }

               private void ThreadRun(object context)

               {

                        ...........

                        RuleContext rulecontext = context as RuleContext context;

                       rulecontext.Completed(); //the completed isn't called on the original call thread.

                        ...............

               }

......................

}

JonnyBee replied on Saturday, September 28, 2013

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:

 

Sam_Lin replied on Saturday, September 28, 2013

Dear Jonny:

         Thanks.

         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?

JonnyBee replied on Sunday, September 29, 2013

No,

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
 http://www.albahari.com/threading/#

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