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 would pass my own custom [ApplicationContext](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.applicationcontext?view=netcore-3.1) to `Application.Run()` in `program.cs`.

This will allow you to have NO INTERFACE until your conditions are met. The application will also continue to run (even when you close the Forms) until you explicitly call [Application.Exit()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.exit?view=netcore-3.1).

You can keep a reference to your Form at class level. This will help you decide if you need to work with the existing one, or create a new one.

Note that I'm using the [System.Windows.Forms.Timer](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer?view=netcore-3.1), not the threaded timer.

Something like...

```
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyContext());
    }
}

public class MyContext : ApplicationContext
{

    private EL.CustomMessageBox l = null;
    private System.Windows.Forms.Timer timer;

    public MyContext()
    {
        timer = new System.Windows.Forms.Timer();
        timer.Interval = (int)TimeSpan.FromMinutes(1).TotalMilliseconds;
        timer.Tick += Timer_Tick;
        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        bool result = true; // hit the database and get an answer
        if (result)
        {
            if (l == null || l.IsDisposed)
            {
                // no form has been created yet, or the previous one was closed
                // create a new instance
                l = new EL.CustomMessageBox();
                l.Show();
            }
            else
            {
                // if we get in here, then the previous form is still being displayed
                // if your form can be minimized, you might need to restore it
                // if (l.WindowState == FormWindowState.Minimized)
                // {
                //     restore the window in here?
                // }
            }

            // update the form "l" with some data?
            l.xxx = yyy;
        }
    }

}

```
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>