1

编辑:accountrepository 的问题-membershipprovider

我目前正在尝试构建一种游戏,我希望有一个无限循环来查找未完成的游戏并更新它们。(每隔几分钟)

下面是我想连续运行的代码,我在 application_start 上调用 Engine 类。当我尝试时收到一个问题,即我的模型尚未创建。

有没有办法等到创建模型然后启动该线程?

提前致谢 !(下面的堆栈跟踪)

“在创建模型时不能使用上下文。”

 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 => p.IsGesimuleerd ==                   false).Count() > 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 => 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.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>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& completedSynchronously) +184 
4

1 回答 1

1

我会建议使用 Windows Service而不是尝试在 Applciation 开始时运行代码

如果不是那么 Ajax Call with Sleep Interval of 60 seconds in Master PageHTML5 Web Sockets

于 2012-06-12T15:18:35.577 回答