Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7679453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:52:51+00:00 2026-05-31T17:52:51+00:00

I have been researching how to create a hybrid winform and CLI app… I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:52:52+00:00Added an answer on May 31, 2026 at 5:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been researching on whether we can create an app which will play
I have been researching AWS. It seems a little confusing to me. We want
I have been researching this problem for a while now and I just can't
I have been researching asynchronous messaging, and I like the way it elegantly deals
I have been researching and playing with functional programming lately, solely to broaden my
I have been researching options for printing report-like data via a web application. Some
I have been researching audit triggers for some of my sql tables. I came
I have been researching how to get Cygwin to work under emacs . I
I have been researching a few different apps lately for my company which are
Been working on a personal add-in for VS 2008 and have been researching automation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.