Question: A form should open only when there is an event if there is no event it should not display on the screen. So Basically i thought of using a timer to do this. An exe will continously be running and after every minute it checks the db to see if there is data and if there is it shows up on the screen and will only be closed manually with user interaction. After a minute it checks again and displays the form if Data is present in the DB.

I used system.threading.Timer in Program.cs file to open a window after every minute.Below is the code

```
timer = new System.Threading.Timer((s) => { 
  EL.CustomMessageBox l = new EL.CustomMessageBox(); 
  l.ShowDialog();                
}, null, TimeSpan.Zero, 60000);

```

After certain time I see that this exe is still running in the taskmanager but even though there is data in the DB it stops showing up on the screen. Any help is appreciated.

Answer: 
I can't help but think that the other answers, massively technically correct as they are, don't actually solve the problem because they probably don't make sense if you aren't aware of how Windows works. Idle\_Mind's is closest to what I'd do, though if the forms designer is familiar I'd go for a solution that basically just uses that - as such I present what I would do to solve the task you're faced with:

* Have an app with one form (or make this form an autonomous one within another app, but for now maybe do it as a dedicated app for simplicity) - make a new Windows Forms project
* Have a Timer (a Windows Forms timer, out of the toolbox, not a System.Threading timer) with an interval of 60000 and Enabled = true
* Have a timer Tick event handler on your form (double click the timer in the tray under the form designer to attach an event handler) that queries the DB and finds if there are any messages
* If there are new messages, adds them to a listbox or something, and calls `this.Show()` to show the form
* Have an eventhandler attached to the FormClosing event so when the user clicks X, the form hides instead of closes:

```
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing) 
    {
        e.Cancel = true;
        Hide();
    }
}

```

* Maybe have the FormClosing event clear the messages listbox. This way if the form opens and the user is on lunch, the messages will build up and build up, then they can read them and clear them by closing the form. Calling Show on an already-visible form does nothing, so the messages will just accumulate into the listbox if more messages come in and the form is already visible

---

Good quick rule of thumb; never use System.Threading Timer in a Windows Forms app. Use a timer out of the forms designer toolbox instead. Only use a threading timer if you're writing a service or Console app etc. For stability reasons, Windows controls absolutely must be accessed by the thread that originally created the control. Windows forms timer is aware of this and its Tick event can safely access the controls (a form is a control, showing it requires to access it) in a Forms app
You should call `Invoke` to execute your delegate on the thread that owns the control's underlying window handle.

Something like this should work:

```
timer = new System.Threading.Timer((s) => { 
  EL.CustomMessageBox l = new EL.CustomMessageBox(); 
  l.Invoke((Action) () => 
  {
    l.ShowDialog();
  });               
}, null, TimeSpan.Zero, 60000);

```

Or even better, use this extension method:

```
public static void InvokeIfRequired(this Control c, MethodInvoker action)
{
    if (c.InvokeRequired)
    {
        c.Invoke(action);
    }
    else
    {
        action();
    }
}

```

And call it like this:

```
l.InvokeIfRequired(() => { l.ShowDialog(); });

```

Further information can be found at: <https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls?view=netframeworkdesktop-4.8>