【问题标题】:Timer running in design mode causing Visual Studio 2017 to crash在设计模式下运行的计时器导致 Visual Studio 2017 崩溃
【发布时间】:2018-04-05 21:59:17
【问题描述】:

我在自定义控件上设置了一个计时器,该控件每 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)

【问题讨论】:

  • 如果您处于设计模式,您可以“不启动”计时器吗?计时器根本不应该运行。
  • 我已将enabled=false 设置在我可以设置的任何地方。我也不认为计时器应该运行,但我在其他地方读过它可以运行。
  • .DesignMode 属性不是最可靠的,尤其是在构造对象时(在构造函数运行之前无法设置.DesignMode),请参阅stackoverflow.com/questions/1166226/… 和@ 987654322@
  • 好吧,让我尝试其中的几种方法,看看它对我有什么好处。谢谢。

标签: c# .net winforms timer


【解决方案1】:

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

  1. 创建一个包含类的对象。 (如果你正在使用一个类)

    TestClass ObjTestClass = new TestClass();

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

    线程 screenthread = new Thread(ObjTestClass.startTimer);
    screenthread.Start();

  3. 在 TestClass 中编写你的 startTimer 函数。

    public void startTimer()
    {
    System.Timers.Timer aTimer = new System.Timers.Timer();
    aTimer.Elapsed += new ElapsedEventHandler(timer_Tick);
    aTimer.Interval = 900000;
    aTimer.Enabled = true;
    }

时间间隔 15 分钟 = 900000

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多