I'm creating a System Tray application. Upon initializing the app, I want to:
- Initialize and load a form so it runs in the background
- But keep the form hidden (until the user doubleclicks the tray icon)
It is important that the form is loaded and runs in the background, because the form contains an embedded browser that will initialize a web socket connection to receive data. But it needs to be hidden. I tried solving this by playing with the Visible
property of the form. So far, I have this (only relevant code shown):
public TrayApp()
{
var ni = new NotifyIcon();
InitializeForm();
ni.DoubleClick += this.ShowForm;
}
private void InitializeForm()
{
//load the form in the background so it can start receiving incoming data, but don't actually show the form
myForm = new MyForm();
myForm.Show();
myForm.Visible = false;
}
private void ShowForm(object sender, EventArgs e)
{
myForm.Visible = true;
}
This works quite well, except for one small detail: upon starting the app, I briefly see the form flashing before it is hidden. I suppose the Show
method also sets the Visible
flag to true
, causing the flash to occur.
Other things I tried, based on comments:
- Do not call
myForm.Show()
, only initialize the form. This avoids the flash but won't load the browser and hence the websocket connection is not initialized - Do
myForm.Hide()
: same effect as previous - Set
Opacity
to0
before callingShow()
and set it to1
after settingVisible
tofalse
: this actually works, but I was hoping for a cleaner solution
How to avoid the flash and keep the form running but hidden?