Can I use Process to execute a console application without showing the command prompt

M

Mullin Yu

But, I want is that I can have a Main app that will start a new process or
kill one particular or all process. The process will open a console exe.
But, I don't want the user to close the console windows by themselves, and
force them to use the Main app.

If I use Windows Services, I'm not sure can I create one dynamically, and
then kill easily. But, if using console application, my following coding is
working now.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

namespace TestOutboundMain
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private System.Windows.Forms.Button button2;

ArrayList arrOutProcess = new ArrayList();
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button3;
int count = 0;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Start Process";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(16, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Kill Process";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(160, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 2;
this.textBox1.Text = "0";
//
// button3
//
this.button3.Location = new System.Drawing.Point(16, 104);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(120, 24);
this.button3.TabIndex = 3;
this.button3.Text = "Kill All Processes";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(280, 149);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button3,
this.textBox1,
this.button2,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void button1_Click(object sender, System.EventArgs e)
{
Process p = new Process();

p.StartInfo.RedirectStandardOutput=false;

p.StartInfo.FileName =
@"C:\test\outboundmain\TestOutboundMainConsole.exe";
p.StartInfo.Arguments = "Outbound" + count.ToString();

p.StartInfo.UseShellExecute = true;
p.Start();

// Add to ArrayList
arrOutProcess.Add(p);

count++;
}

private void button2_Click(object sender, System.EventArgs e)
{
try
{
//p.Kill();
Process tmpProcess = (Process)
arrOutProcess[Convert.ToInt32(textBox1.Text)];
tmpProcess.Kill();
// arrOutProcess.RemoveAt(i)
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}

}

private void button3_Click(object sender, System.EventArgs e)
{
for(int i=0; i<count; i++)
{
Process tmpProcess = (Process) arrOutProcess;
if(!tmpProcess.HasExited)
{
tmpProcess.Kill();
}
}
}


}
}
 
D

Daniel O'Connell

Set UseShellExecute in ProcessStartInfo to false, that should hide the
console window.

Mullin Yu said:
But, I want is that I can have a Main app that will start a new process or
kill one particular or all process. The process will open a console exe.
But, I don't want the user to close the console windows by themselves, and
force them to use the Main app.

If I use Windows Services, I'm not sure can I create one dynamically, and
then kill easily. But, if using console application, my following coding is
working now.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

namespace TestOutboundMain
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private System.Windows.Forms.Button button2;

ArrayList arrOutProcess = new ArrayList();
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button3;
int count = 0;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Start Process";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(16, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Kill Process";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(160, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 2;
this.textBox1.Text = "0";
//
// button3
//
this.button3.Location = new System.Drawing.Point(16, 104);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(120, 24);
this.button3.TabIndex = 3;
this.button3.Text = "Kill All Processes";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(280, 149);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button3,
this.textBox1,
this.button2,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void button1_Click(object sender, System.EventArgs e)
{
Process p = new Process();

p.StartInfo.RedirectStandardOutput=false;

p.StartInfo.FileName =
@"C:\test\outboundmain\TestOutboundMainConsole.exe";
p.StartInfo.Arguments = "Outbound" + count.ToString();

p.StartInfo.UseShellExecute = true;
p.Start();

// Add to ArrayList
arrOutProcess.Add(p);

count++;
}

private void button2_Click(object sender, System.EventArgs e)
{
try
{
//p.Kill();
Process tmpProcess = (Process)
arrOutProcess[Convert.ToInt32(textBox1.Text)];
tmpProcess.Kill();
// arrOutProcess.RemoveAt(i)
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}

}

private void button3_Click(object sender, System.EventArgs e)
{
for(int i=0; i<count; i++)
{
Process tmpProcess = (Process) arrOutProcess;
if(!tmpProcess.HasExited)
{
tmpProcess.Kill();
}
}
}


}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top