Print Page | Close Window

Serial Coroutines and ManagedThreadId

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2277
Printed Date: 27-Mar-2025 at 10:37pm


Topic: Serial Coroutines and ManagedThreadId
Posted By: stephenmcd1
Subject: Serial Coroutines and ManagedThreadId
Date Posted: 29-Oct-2010 at 12:55pm
I've run into problems when using Serial Coroutines with respect to the ManagedThreadId property on the EntityManager.  The exception specifically says that I should "Consider calling the EntityManager’s asynchronous methods; they work safely on background threads managed by DevForce. "  I thought I was using the asynchronous methods so I was expecting it to work.  An example of the code is as follows:

        public void TestCoroutine(AsyncEventCompletedCallback<GenericAsyncEventArgs<int>> callback)
        {
            //Get the entity manager from somewhere
            var myEntityManager = Uow.EntityManager;

            Coroutine.Start(
                () => BuildBasicCoroutine(myEntityManager),
                op =>
                    {
                        //Always fails
                    });
        }

        private static IEnumerable<INotifyCompleted> BuildBasicCoroutine(DataModelEntityManager em)
        {
            yield return em.Users.ExecuteAsync();
            yield return em.Contracts.ExecuteAsync();
        }

If I switch it to use Coroutine.StartParallel it will succeed just fine but in many cases I actually need serial behavior becaues one query depends on a previous query.  

It seems like this should be possible and maybe I'm just doing something basic wrong.  Any help would be appreciated.



Replies:
Posted By: kimj
Date Posted: 29-Oct-2010 at 3:52pm
It should be possible with serial execution, and does work in our tests.
 
I can think of two reasons why you might get this error:
  • The Uow.EntityManager was constructed on another thread, or
  • You're running in a test environment (or a console application) which does free threading and doesn't synchronize threads to the context. 


Posted By: stephenmcd1
Date Posted: 01-Nov-2010 at 8:27pm
Thanks, I was testing this in two place.  One was a unit test which we expect to fail.  The other was in a Silverlight app but I found that it was something else that was messing with my entity manager so mystery solved.  Thanks for the help.



Print Page | Close Window