0

I am updating a label of a popup every 5 minutes and have based the code on this article.

public MyPopup()
{
   var startTimeSpan = TimeSpan.Zero;
   var periodTimeSpan = TimeSpan.FromMinutes(5);

   var timer = new System.Threading.Timer((e) =>
   {
     UpdatePopupUI();
   }, null, startTimeSpan, periodTimeSpan);
}

Everything works fine but when I get back to the main windows and open new windows (not a new instance of the popup) it seems that the thread get deleted. I get no error in the input windows. Any idea regarding what can be killing the thread? Is there a way that I can troubleshoot this?

Salim
  • 495
  • 3
  • 20

1 Answers1

1

Most probably your objects get garbage collected. Store your timer variable permanently, maybe in a static variable.

PepitoSh
  • 1,774
  • 14
  • 13
  • Or you can use [`GC.KeepAlive(timer)`](https://msdn.microsoft.com/en-us/library/system.gc.keepalive(v=vs.110).aspx) explictly - but that is probably not appropriate for this particular code. – Matthew Watson May 30 '18 at 07:34