C# Hiding form when running form program from the

2019-07-25 11:24发布

I'm working on a program that is form based but I have been asked to add some command line support, this works fine apart from the form flashes up and closes down when running from the command line. Is there anyway to hide the form whilst the command line is running? Some code does refer to controls so the gui would need to be accessible but not visible, is this workable?

2条回答
时光不老,我们不散
2楼-- · 2019-07-25 12:22

There are a few apps that I've run across which have this behavior right from the constructor of the form class. Basically their constructor handles the command line arguments and the calls an Environment.Exit(0) when the process for the command line args has completed successfully. Calling Environment.Exit before the constructor of the form class complete effectively prevents the form from ever showing up.

Having said that, it's error prone, and I would say that it's been to not have the form class as the startup of the app. Use another class instead which can instantiate and show the form as needed, or process the command line arguments instead.

查看更多
▲ chillily
3楼-- · 2019-07-25 12:30

In your Main method you can create the form such as:

main = new MainForm();

then do any command line processing required.

When not in command line mode simply call:

Application.Run(main);
查看更多
登录 后发表回答