简体   繁体   中英

Is it possible to run a Windows app within a console window?

I have a Windows app that I've written in C# 4 and WPF; I've now been asked if I can add a commandline parameter (eg /console) that would force it to run as a console app so it can be run by a task scheduler.

Is this possible with modern apps? Or do I need to create a separate console app?

UPDATE : can I just emphasise that this is a WPF application. There is no convenient static void Main(string[] args) entry point to hook into. But the PM would still like the app to have the ability to run from the commandline...

FINAL UPDATE : the trick, as pointed out by @RodH257, is that the WPF app codegens the expected static void Main. You can add your own class with a method of the same name and in the project build properties, set it as the startup object for the executable. You'll also need the [STAThread] attribute on the method so that WPF will run properly too.

Yes, a Windows GUI app has a Main just like a console app. So the trick is to do some pre-processing Main before starting the UI loop. It should be noted that if you want to use the console from inside an app compiled as a .NET "Windows Application" in Visual Studio, you will have to AllocConsole manually since you won't get one by default.

This MSDN forum thread discusses some more about "hybrid console/GUI apps".

You can launch it from within a console app using a dos command, but, unless you modify the startup routine (in Main() ) when it runs it is going to load and display the main window... If you don't want the main window to be displayed, just recode the main routine so that a Main() parameter disables that option...

Instead of this:

    [STAThread]
    static void Main()
    {
       Application.Run(new MainForm());
    }

code this:

    [STAThread]
    static void Main(params string[] args)
    {
         if(args.length <= 1)
              Application.Run(new MainForm());
         else if (args[1].StartsWith("C")) // "C" for "C"onsole version
              // Do whatever needs to be done to start 
              //    functionality for console app version 
    }

Then, when you run it, add the /C switch:

   C:\MyCurrentDirectory>MyWinFormsApplicationName.exe /C

You can either turn it into a console application and manually show the first WPF dialog. just as you would if you were creating a DLL and starting a WPF window, as per below

 static class Program
{
    static void Main(params string[] args)
    {
        if (args.Length > 0)
        {
           //do stuff here
            return;
        }
          Window1 window = new Window1();
          window.ShowDialog();
    }
}

OR, have a look at this link for editing the entrypoint on your current WPF project and act based on arguments How to write custom Main method for a WPF application?

EDIT: Updated the post to make it more clear for the exact situation described.

It's extremely dependent upon how the program was originally written.

If you have written your logic in a loosly coupled way, you can create another console application and reuse your logic there. It's manual programming/refactoring work.

If it gets run by a task scheduler, you don't need any screen at all to come up. remove the startupuri and manually launch the windows depending on commmand line.

if its a console launched process, just do whatever processing you need. Otherwise, launch your windows...

Here's an article/code sample showing how it's done . The build type is important. Your compiler only lets you have one build target (eg, GUI, command-line).

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