Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I launch .net windows forms application with no visible windows?

I have a .net windows forms application that needs to open directly to the notify icon (system tray) with no visible windows. I realize that I can do this in the onshown event or something like it. But if I do that I get a flash of the window. How can I avoid that flash? I have tried modifying my Program.cs file to look like this:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

MainForm frm = new MainForm();
frm.Visible = false;
Application.Run(frm);

However this doesn't work either because Application.Run() makes the form visible. I am pretty sure there is an easy answer that I am missing. Any help is greatly appreciated.

like image 737
Jason Webb Avatar asked Feb 26 '23 21:02

Jason Webb


2 Answers

There's an overload for Application.Run() that takes in no parameters, and thus doesn't immediately show a form on application launch. Of course, you'll have to manage what causes the application to terminate yourself since there's no initial or 'main' form for it to monitor. So for example it'd be your notification icon, which I'm sure you'll be able to handle.

like image 100
BoltClock Avatar answered Mar 01 '23 15:03

BoltClock


If you don't need a main form at the time you start your application, here is a link to an article that describes how to create just a NotifyIcon.

like image 38
Niall C. Avatar answered Mar 01 '23 16:03

Niall C.