title: Infinite asynchronous method (How to wait until context model is created?)
body: <p>EDIT: Problem with accountrepository - membershipprovider</p>
  
  <p>I am currently trying to build a type of game where i want to have an infinite loop that looks for unfinished games and updates them. (Every couple of minutes)</p>
  
  <p>Below is the code i would like to run continuously and i call the Engine class on application_start. When i try that i receive a problem that my model hasn't been created yet.</p>
  
  <p>Is there a way to <strong><em>wait untill the model is created</em></strong> and <strong>then</strong> start that thread?</p>
  
  <p>Thanks in advance ! (stacktrace below)</p>
  
  <p>"The context cannot be used while the model is being created."</p>
  
  <pre><code> public class VluchtEngine
      {
          private VluchtRepository _vluchtRepository;
  
          private TimeSpan WaitTime = new TimeSpan(0, 0,30);
  
          public VluchtEngine(PigeonFancierContext c)
  
          {
              _vluchtRepository = new VluchtRepository(c);
  
  
              Task t2 = Task.Factory.StartNew(Run);
  
  
              }
  
          public void Run()
          {
  
              while (/*_vluchtRepository.FindAll().Where(p =&gt; p.IsGesimuleerd ==                   false).Count() &gt; 0)*/true)
              {
                  foreach (var vlucht in _vluchtRepository.FindAll())
                  {
                      foreach (var duif in vlucht.Inschrijvingen)
  
                      {
                          duif.AfgelegdeAfstand += 1;
                          if (duif.AfgelegdeAfstand == vlucht.Afstand)
                              duif.IsAangekomen = true;
                      }
  
                      if(vlucht.Inschrijvingen.Where(p =&gt; p.IsAangekomen).Count() == 0)
                      {
                          vlucht.IsGesimuleerd = true;
                      }
                  }
  
                  _vluchtRepository.SaveChanges();
  
  
                  Thread.Sleep(WaitTime);
  
  
              }
  
          }
  
  
  }
  }
  
  
  
  
  
  [InvalidOperationException: The context cannot be used while the model is being created.]
     System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +577466
     System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
     System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +63
     System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15
     System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +37
     System.Linq.Queryable.Where(IQueryable`1 source, Expression`1 predicate) +63
     PigeonFancier.Infrastructuur.MelkerRepository.FindByEmail(String email) in C:\Users\Jonas\Documents\Visual Studio 2010\Projects\PigeonFancier\PigeonFancier\Infrastructuur\MelkerRepository.cs:39
     PigeonFancier.Infrastructuur.MelkerModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) in C:\Users\Jonas\Documents\Visual Studio 2010\Projects\PigeonFancier\PigeonFancier\Infrastructuur\MelkerModelBinder.cs:22
     System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317
     System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117
     System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
     System.Web.Mvc.Controller.ExecuteCore() +116
     System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
     System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
     System.Web.Mvc.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5() +37
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +21
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) +12
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
     System.Web.Mvc.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d() +50
     System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f) +7
     System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +184 
  </code></pre>