How to Run a Winform from Console Application

how to run a winform from console application?

The easiest option is to start a windows forms project, then change the output-type to Console Application. Alternatively, just add a reference to System.Windows.Forms.dll, and start coding:

using System.Windows.Forms;

[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form()); // or whatever
}

The important bit is the [STAThread] on your Main() method, required for full COM support.

c# display windows form from console application

The easiest option is to start a windows forms project, then change the output-type to Console Application. Alternatively, just add a reference to System.Windows.Forms.dll, and start coding:

using System.Windows.Forms;

[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form()); // or whatever
}

The important bit is the [STAThread] on your Main() method, required for full COM support.

Another way :

using System.Runtime.InteropServices;

private void Form1_Load(object sender, EventArgs e)
{
AllocConsole();
}

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

If you research this topic , just on Google you can find so much way to implement your question : my suggest is to try some method and verify what is more adapt to you.

How do I run my Console Application code within a Windows Forms?

I can mention two options of many:

  1. Deal with your Console app as a DLL and reference it in your WinForm Runner App.
  2. Deal with your Console app as an EXE file and run it using Process.Start.

The first option will give you the ability to reach the methods while the second one will deal with it as a totally separate app with all its functionalities as a single application.

Example of the Second Option:

  1. Create a Solution with three projects as in the screenshot below.

Solution


  1. Suppose your Console App 1 code is
    using System;
namespace SOC.ConsoleApp01
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Written from Console App 1");
Console.ReadLine();
}
}
}

While the second is

using System;

namespace SOC.ConsoleApp02
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Written from Console App 2");
Console.ReadLine();
}
}
}

  1. Now build the Console App1 and Console App 2 by right click on each of them in the solution explorer and click Build or ReBuild.
    Build

This will result in execution files generated like in the following screenshots:
App 1 Exe file
App 2 Exe file


  1. In your WinForm Project write the code of Subject buttons like this
    using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace SO.Console.Apps.Runner
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Process.Start(@"C:\REPOS\SO.Console.Apps.Runner\SOC.ConsoleApp01\bin\Debug\SOC.ConsoleApp01.exe");
}

private void button2_Click(object sender, EventArgs e)
{
Process.Start(@"C:\REPOS\SO.Console.Apps.Runner\SOC.ConsoleApp02\bin\Debug\SOC.ConsoleApp02.exe");
}
}
}

the result is
Result

how to execute console application from windows form?

If you are just wanting the console window to stay open, you could run it with something like this command:

System.Diagnostics.Process.Start( @"cmd.exe", @"/k c:\path\my.exe" );

How do I show a console output/window in a forms application?

this one should work.

using System.Runtime.InteropServices;

private void Form1_Load(object sender, EventArgs e)
{
AllocConsole();
}

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

Can a WinForms program be run from command line?

Of course it is possible, if you have built your winform app using the default settings, then search the Program.cs file and you will find the Main method.

You could change this method signature in this way

    [STAThread]
static void Main(string[] args)
{
// Here I suppose you pass, as first parameter, this flag to signal
// your intention to process from command line,
// of course change it as you like
if(args != null && args[0] == "/autosendmail")
{
// Start the processing of your command line params
......
// At the end the code falls out of the main and exits
}
else
{
// No params passed on the command line, open the usual UI interface
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());

}
}

I have forgot to answer the other part of your question, how to launch this winapp from your console application. It is really easy with the Process class in the System.Diagnostics namespace and the ProcessStartInfo to fine tune the environment of the launched application

   ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "YourWinApp.exe";
psi.Arguments = "/autosendmail destination@email.com ..... "; // or just a filename with data
psi.WorkingDirectory = "."; // or directory where you put the winapp
Process.Start(psi);

Given the numerous information needed to send a mail, I suggest to store all the destination addresses and texts to send in a file and pass just the filename to your winapp

visual studio open windows form from console application c#

Use the same code a WindowsForms project does, Application.Run is still the way to go:

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

Hard to say what you did wrong with your original attempt at that, as you didn't post that code.

Run a windows form while my console application still running

try this it work on me

using System.Windows.Forms;
using System.Threading;

namespace ConsoleApplication1
{

class Program
{

public static bool run = true;
static void Main(string[] args)
{

Startthread();
Application.Run(new Form1());
Console.ReadLine();


}

private static void Startthread()
{
var thread = new Thread(() =>
{

while (run)
{
Console.WriteLine("console is running...");
Thread.Sleep(1000);
}
});

thread.Start();
}
}
}

Threading is like "process inside a process" in my own understanding.



Related Topics



Leave a reply



Submit