简体   繁体   中英

C# hybrid cli and win form app

I have been researching how to create a hybrid winform and CLI app... I started out my app as Winforms, now I am adding CLI to it... It seems to work but has a few issues I want to figure out how to fix and have not been able to do so , probably due to my lack of experience with C#.

If i have output type in VS set to "windows application", and use the code below i am able to run the winform portion for GUI, and also from command line am able to give it parameters and it works, well at least it outputs consolewrites i have coded in, i have a seperate c# class file that has my main code so it is seperate from winform GUI and my eventual cli code, they both will just feed user input to this other "main" c# class.. anyway here is the code.

     [STAThreadAttribute]

    [DllImport("kernel32.dll")]
    static extern bool AttachConsole(int dwProcessId);
    private const int ATTACH_PARENT_PROCESS = -1;

    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new form_Main());
        }
        else
        {   
            AttachConsole(ATTACH_PARENT_PROCESS);

            cli_main cli = new cli_main();
            cli.start_cli(args);
        }
    }

well it works in gui, i can access my menus and create different win forms, moment i click a button to perform an action i get the following exception:

System.Threading.ThreadStateException was unhandled by user code Message=Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

if i then change the output type to "console application", it works perfectly in all manners in cli and GUI, BUT when it opens the winform/GUI portion i get this ugly CMD window that will not go away.. here is the code i used, basically just what i started with before i added the above code.

    static void Main(string[] args)
    {


        if (args.Length == 0)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new form_Main());
        }
        else
        {            
            cli_main cli = new cli_main();
            cli.start_cli(args);
        }
    }

again with my lack of knowledge on C# i am hoping someone can point me to a solution . I would prefer to keep the app as output "console application" and find a way to hide the console that is opened when i start the winform/gui portion..?? thanks in advance.

Did you do what it said in the error message?

Ensure that your Main function has STAThreadAttribute marked on it

In the code you pasted, the STAThread attribute is not marking the Main method, it is marking the AttachConsole external function. Move that to where it ought to be and you shouldn't have any problems.

If your application is a console application, it will get a console window automatically if there isn't already one attached. That's the point of making it a console application. You can use FindWindow and ShowWindow(SW_HIDE) to hide it at runtime but it will still flash on-screen briefly.

If you plan to start your application from an existing console window most of the time, you should keep it as a console application, since it will inherit the parent process's console window by default. If you plan to start your application from a UI shortcut or from other non-console processes, you should probably make it a Windows application and allocate the console as needed.

Thanks for all, but some changes for previous pattern

It works so:

static void Main(string[] args)
{
    // Test pattern
    args = new string[] { "-flag1", "value1", "-flag2", "value2", "-flag3", "value3" };

    if (args.Length == 0)
        MainWinApp();
    else
        MainCLI(args);

}

[STAThread]
static MainWinApp()
{
    // Your code for start GUI application
}
[STAThreadAttribute]
static void MainCLI(string[] args)
{
    // Your code for CLI application
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM