Behavior in WinForm/Console Hybrid Application

后端 未结 4 2033
走了就别回头了
走了就别回头了 2021-01-20 14:28

I have a WinForm project that I want to be usable as a console app if certain arguments are passed into it. Using some tips I read from here, I used the following code to m

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-20 14:48

    Instead of this iffy method, you can accomplish this by just creating a winforms application and not displaying the main form if a certain argument is passed on the command line.

    Since a console application doesn't need a message loop, you don't call Application.Run() when called in console mode.

    So your Main() would be something like:

        static void Main(params string[] args)
        {
            if (args.Length > 0 && args[0] == "consolemode")
            {
                // do stuff 
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
        }
    

    I haven't tried this but I think it would work.

提交回复
热议问题