If I'm right, you want to close the form when the timer stops.
So I do it:
System.Threading.Timer formTimer;
I use a boolean to see if the timer is on
public Boolean active { get; set; }
Create this function:
public void timerControl() { if (!active) formTimer = new System.Threading.Timer(new TimerCallback(TimerProc)); try { formTimer.Change(REFRESH_STATUS_TIME_INTERVAL, 0); } catch {} active = true; }
To end the timer, you need the TimerProc function, which is called when creating a new timer:
private void TimerProc(object state) { System.Threading.Timer t = (System.Threading.Timer)state; t.Dispose(); try { CloseScreen(); } catch{} }
To make programming easier for me, I made the CloseScreen () function:
public void CloseScreen() { if (InvokeRequired) { this.Invoke(new Action(CloseScreen), new object[] { }); return; } active = false; Close(); }
Put all these functions in your form class and just use timerControl to activate the timer. You can access it from your Manager class: Form1.TimerControl(); Or put it in an event handler, successfully!
source share