New Posts New Posts RSS Feed: Problem with running more asynchronous queries
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Problem with running more asynchronous queries

 Post Reply Post Reply
Author
mpetkovic View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Sep-2010
Location: The Netherlands
Posts: 1
Post Options Post Options   Quote mpetkovic Quote  Post ReplyReply Direct Link To This Post Topic: Problem with running more asynchronous queries
    Posted: 13-Sep-2010 at 2:56am
I have noticed the unstable behavior when I try to work with queries asynchronously.

When I start a new query before the completion of the previous one the application does not show stable behavior (sometimes it crashes and sometimes not).
But If I start the new query after the completion of the previous one the application is then stable.

Please find below the part of code where I pointed out when the problem occurs and when not. The first query is here the employees from the Northwind database and the queries that should be executed after  
the first one are put into the method called "then_multiple_async_anonymous_queries_work".

This code comes from a small example project I have made to illustrate this problem. You can get it from me of course if you want to run it.
Any comments/ideas about this problem are welcome.


//Module: BusinessApplicationModule
//file: BusinessApplicationModuleViewModel.cs


private NorthwindIBEntities _mgr = new NorthwindIBEntities();

*****************************************************

//ViewModel constructor:
public BusinessApplicationModuleViewModel() {
     .......

 var query = _mgr.Employees;
 var op = query.ExecuteAsync();
            op.Completed += (s, args) => {
              if (args.Error == null) {
                args.Results.ForEach(Employees.Add);
                CurrentEmployee = Employees.FirstOrDefault();
                RaisePropertyChanged("CurrentEmployee"); //Consider moving into the property
                //then_multiple_async_anonymous_queries_work(); ///works well
               }
              else
              {
                throw new InvalidOperationException(
                  "Server error during query: " + args.Error.Message);
              }
            };

            then_multiple_async_anonymous_queries_work(); //unstable behaviour

        }
***********************************************************

        public void then_multiple_async_anonymous_queries_work()
        {          

                var fetchCount = 0;              
                _mgr.Fetching += delegate { fetchCount++; };              
              
                const int iterations = 100;
                int countDownCounter = iterations;

                var locker = new Object();

                Action<BaseOperation, IEnumerable> countDown = (op, results) =>
                {
                    LogFetchingData.Add("In callback for " + op.UserState);
                    lock (locker)
                    {
                        countDownCounter--;
                        if (0 >= countDownCounter)
                        {
                            LogFetchingData.Add("*** Fetch Count = " + fetchCount);

                        }
                    }
                };

                for (var i = 0; i < iterations; i++)
                {
                    _mgr.Employees.Select(emp => new { emp.EmployeeID, emp.Orders.Count })
                      .With(QueryStrategy.DataSourceOnly)
                      .ExecuteAsync(op => countDown((BaseOperation)op, (op.HasError) ? null : (IEnumerable)op.Results), i);
                }
         }

           
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post Posted: 15-Sep-2010 at 3:05pm
Have you migrated to version 6.0.5 yet?  This version of DevForce 2010 includes a large number of enhancements and bug fixes.  It also contains breaking changes with respect to Asynchronous Operations:
 •    Exceptions Thrown by Asynchronous Operations Must Be Handled. All asynchronous persistence operation exceptions now conform with Microsoft RIA Service style of exception handling. 
 •    EntityManager.AuthorizedThreadId Property. There is a new AuthorizedThreadId property on the EntityManager that checks to see that the EntityManager is used in a thread-safe manner.
See Release Notes for details. 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down