473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to implement thread for individual button

I want to create a C# GUI application that will start a new console
application, e.g. console.exe when clicking a button. Then, if click a
button, it will kill a specific console application.

Am I right that I need to create a new thread when clicking a "Start Button"
for starting a console application.

And, a separating thread of "Stop Button" is used to stop a specific console
application?? I tried to do the following, button the MessageBox inside
Button2 (Stop Button) didn't display. I'm afraid that since the following is
a single thread GUI application.

How to change the following to adapt to my design?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

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 int processID;
private System.Windows.Forms.Button button2;
System.Diagnostics.Process p;

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.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 88);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(104, 136);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(280, 181);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
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)
{
p = new System.Diagnostics.Process();

p.StartInfo.RedirectStandardOutput=false;

p.StartInfo.FileName =
@"C:\test\outboundmain\TestOutboundMainConsole.exe ";

p.StartInfo.UseShellExecute=true;
p.Start();
processID = p.Id;
p.WaitForExit();
MessageBox.Show("exit!!");
p.Dispose();
}

private void button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Start to kill the process");
p.Close();
MessageBox.Show("Try to close the process");
p.Dispose();
MessageBox.Show("Try to dispose the process");
p.Kill();
MessageBox.Show("Try to kill the process");

}
}
}
Nov 15 '05 #1
1 5909
Hi Mullin,

I'd rather say you should create a separate process rather than a separate
thread. You should then store the instance of the Process class somewhere
and ask the process to exit upon second button click. I have taken a glance
on the snippet provided and it seems to do the job.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Mullin Yu" <mu*******@ctil.com> wrote in message
news:eo**************@TK2MSFTNGP12.phx.gbl...
I want to create a C# GUI application that will start a new console
application, e.g. console.exe when clicking a button. Then, if click a
button, it will kill a specific console application.

Am I right that I need to create a new thread when clicking a "Start Button" for starting a console application.

And, a separating thread of "Stop Button" is used to stop a specific console application?? I tried to do the following, button the MessageBox inside
Button2 (Stop Button) didn't display. I'm afraid that since the following is a single thread GUI application.


Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matt | last post by:
i need to implement a clear button to clear all the fields in the form, but i am thinking i can just use reset button. <input type="reset" name="reset" value="CLEAR"> The first thought is...
1
by: JLOI | last post by:
Hi All, I am trying to implement a dropdown-button control in C# which provides a list of actions. Many Microsoft applicaitons use this kind of control. One example is in the V.S IDE's file...
2
by: Jurjen de Groot | last post by:
I would like to implement a button which will, upon click, print the current webpage. I have no idea where to begin, I think I need some sort of client scripting but I suppose there's a simple...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
1
by: holysmokes99 | last post by:
I am trying to implement a cancel button on my application that ingests a bunch of data from a text file into a database. I have not worked too much with threads, so I am having some trouble. I had...
19
by: darrel | last post by:
On my vb.net page, I have 4 sets of inputs + form buttons. example: Search: (GO) Zip: (GO) County: (GO) County: (GO) The problem is if I go to the page, type in a zip code, and hit...
2
by: conckrish | last post by:
Hi all, This is all about ASP.NET WEB APPLICATION... I have a Button and when i click that button... i need to open the File Dialog through Which i have to select the Image Path... And When...
3
by: koonda | last post by:
Hi Guys, I have 5 listbox controls which I populated from the database tables. I tried it using the wizard. For all 5 listboxes and a single Repeater I used a SQL DataSourse Object and mapped this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.