1

I have a timer setup on a custom control that refreshes project numbers every 15 minutes. I started noticing that Visual Studio would randomly crash. Well, what seemed random, at the time, anyway. I started looking through the event logs and found that the timer on my control was executing. This was throwing an error due to a connection/entity framework lazy loading issue which then caused VS to crash.

Seeing that i'm using !this.DesignMode below, what else can I do to keep this from running

Here's my timer_tick event:

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

Here's the exception text from the event log:

    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)
Tony Evans
  • 163
  • 2
  • 10
  • Can you just "not start" the timer if you are in design mode? The timer shouldn't be running at all. – Ron Beyer Apr 05 '18 at 22:05
  • I've set `enabled=false` everywhere i can. I didn't think the timer should run either, but I've read elsewhere that it can. – Tony Evans Apr 05 '18 at 22:12
  • 2
    The `.DesignMode` property isn't the most reliable thing, especially when the object is being constructed (can't set `.DesignMode` until after the constructor is run), see https://stackoverflow.com/questions/1166226/detecting-design-mode-from-a-controls-constructor and https://stackoverflow.com/questions/336817/how-can-i-detect-whether-a-user-control-is-running-in-the-ide-in-debug-mode-or – Ron Beyer Apr 05 '18 at 22:14
  • Alright, let me try a couple of those approaches and see where it gets me. Thanks. – Tony Evans Apr 05 '18 at 22:43

2 Answers2

1

You can create new Thread for the task. Example :-

  1. create a object of class that contains. (if you are using a class)

    TestClass ObjTestClass = new TestClass();

  2. then create a new Thread and call your timer funtion in this thread.

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

  3. write your startTimer funtion in TestClass.

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

time interval 15 min = 900000

Aakash Singh
  • 1,032
  • 8
  • 27
1

It's a bit late but came across this question after encountering the problem in one of my own apps. It seems the crash occurs when the form designer ends up executing code that contains a dependency only available when the application is running.

I had a similar issue with a user control which has an event hander for the visisblechanged event to enable a timer control when it was made visible in the application. The tick event for the timer would pull data from a database and refresh the control with the new data. This was fine when the app is running but not in the designer.

Darren
  • 26
  • 2