计时器在设计模式下运行,导致 Visual Studio 2017 崩溃



我在自定义控件上设置了一个计时器,该控件每 15 分钟刷新一次项目编号。我开始注意到Visual Studio会随机崩溃。好吧,无论如何,当时似乎是随机的。我开始查看事件日志,发现我的控件上的计时器正在执行。由于连接/实体框架延迟加载问题,这引发了错误,然后导致 VS 崩溃。

看到我在下面使用!this.DesignMode,我还能做些什么来防止它运行

这是我timer_tick事件:

    private void timer_Tick(object sender, EventArgs e)
    {
        if (!this.DesignMode) {
            LoadProjectNumbers();
        }
    }

下面是事件日志中的异常文本:

    Application: devenv.exe
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.InvalidOperationException at    
    System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at    System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(System.Type)
   at System.Data.Entity.Internal.Linq.InternalSet`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.OrderByDescending[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Linq.IQueryable`1<System.__Canon>, System.Linq.Expressions.Expression`1<System.Func`2<System.__Canon,Int32>>)
   at ACGICore.Controls.ProjectNumberSearch.LoadProjectNumbers()
   at ACGICore.Controls.ProjectNumberSearch.timer_Tick(System.Object, System.EventArgs)
   at System.Windows.Forms.Timer.OnTick(System.EventArgs)
   at System.Windows.Forms.Timer+TimerNativeWindow.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr, Int32, IntPtr, IntPtr)

您可以为任务创建新的线程。例:-

  1. 创建一个包含的类的对象。(如果您使用的是类(

    TestClass ObjTestClass = new TestClass((;

  2. 然后创建一个新线程并在此线程中调用计时器函数。

    Thread

    screenthread = new Thread(ObjTestClass.startTimer(;
    屏幕线程。开始((;

  3. 在 TestClass 中编写 startTimer 函数。

    公共无效启动计时器((

    {System.Timers.Timer aTimer = new System.Timers.Timer((;
    aTimer.Elapsed += new ElapsedEventHandler(timer_Tick(;
    a计时器间隔 = 900000;
    aTimer.Enabled = true;
    }

时间间隔 15 分钟 = 900000

有点晚了,但在我自己的一个应用程序中遇到问题后遇到了这个问题。当表单设计者最终执行包含仅在应用程序运行时可用的依赖项的代码时,似乎会发生崩溃。

我有一个类似的问题,一个用户控件有一个用于 visisblechange 事件的事件处理程序,以便在计时器控件在应用程序中可见时启用计时器控件。计时器的 tick 事件将从数据库中提取数据,并使用新数据刷新控件。当应用正在运行但不在设计器中运行时,这很好。

最新更新