Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2006, 05:40 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 224
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to ashu_from_india Send a message via Yahoo to ashu_from_india
Default Contrlling other .NET Application from my .NET app

hi all

i m starting a Windows based .NET app from my Server (windows based .NET app) app using
Process.Start("C:\Test.exe")

now, i want to control this new application (this new app does not hv any user interface)

1) How to close this app from my app?
2) How to know if this app stops running or closes itself due to some error?




thnx in advance...cheers :)

Ashu


 
Old November 28th, 2006, 12:38 AM
Authorized User
 
Join Date: Feb 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to karthiklsimha
Default

Hi Ashu,

Try the below C# code its self explanatory.

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

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

        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.btnStart = new System.Windows.Forms.Button();
            this.btnKill = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // btnStart
            //
            this.btnStart.Location = new System.Drawing.Point(128, 80);
            this.btnStart.Name = "btnStart";
            this.btnStart.TabIndex = 0;
            this.btnStart.Text = "Start";
            this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
            //
            // btnKill
            //
            this.btnKill.Location = new System.Drawing.Point(128, 144);
            this.btnKill.Name = "btnKill";
            this.btnKill.TabIndex = 1;
            this.btnKill.Text = "Kill";
            this.btnKill.Click += new System.EventHandler(this.btnKill_Click);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.btnKill);
            this.Controls.Add(this.btnStart);
            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());
        }

        int intProcessId = 0;
        private void btnStart_Click(object sender, System.EventArgs e)
        {
            System.Diagnostics.Process objProcess = new System.Diagnostics.Process();
            objProcess.EnableRaisingEvents = true;
            objProcess.StartInfo.FileName = "Notepad.exe";
            objProcess.Start();
            intProcessId = objProcess.Id;
            objProcess.Exited +=new EventHandler(objProcess_Exited);
        }

        private void objProcess_Exited(object sender, EventArgs e)
        {
            System.Diagnostics.Process objExitedProcess;
            objExitedProcess = (System.Diagnostics.Process) sender;
            MessageBox.Show("Application Closed with process ID " + objExitedProcess.Id.ToString());
        }

        private void btnKill_Click(object sender, System.EventArgs e)
        {
            System.Diagnostics.Process objProcess = System.Diagnostics.Process.GetProcessById(intProce ssId);
            objProcess.Kill();
        }

    }
}

VB.NET Code

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Diagnostics
Namespace ProcessSampleCS

    Public Class Form1
        Inherits System.Windows.Forms.Form
        Private btnStart As System.Windows.Forms.Button
        Private btnKill As System.Windows.Forms.Button
        Private components As System.ComponentModel.Container = Nothing

        Public Sub New()
            InitializeComponent()
        End Sub

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        Private Sub InitializeComponent()
            Me.btnStart = New System.Windows.Forms.Button
            Me.btnKill = New System.Windows.Forms.Button
            Me.SuspendLayout()
            Me.btnStart.Location = New System.Drawing.Point(128, 80)
            Me.btnStart.Name = "btnStart"
            Me.btnStart.TabIndex = 0
            Me.btnStart.Text = "Start"
            AddHandler Me.btnStart.Click, AddressOf Me.btnStart_Click
            Me.btnKill.Location = New System.Drawing.Point(128, 144)
            Me.btnKill.Name = "btnKill"
            Me.btnKill.TabIndex = 1
            Me.btnKill.Text = "Kill"
            AddHandler Me.btnKill.Click, AddressOf Me.btnKill_Click
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 273)
            Me.Controls.Add(Me.btnKill)
            Me.Controls.Add(Me.btnStart)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)
        End Sub

        <STAThread()> _
        Shared Sub Main()
            Application.Run(New Form1)
        End Sub
        Private intProcessId As Integer = 0

        Private Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim objProcess As System.Diagnostics.Process = New System.Diagnostics.Process
            objProcess.EnableRaisingEvents = True
            objProcess.StartInfo.FileName = "Notepad.exe"
            objProcess.Start()
            intProcessId = objProcess.Id
            AddHandler objProcess.Exited, AddressOf objProcess_Exited
        End Sub

        Private Sub objProcess_Exited(ByVal sender As Object, ByVal e As EventArgs)
            Dim objExitedProcess As System.Diagnostics.Process
            objExitedProcess = CType(sender, System.Diagnostics.Process)
            MessageBox.Show("Application Closed with process ID " + objExitedProcess.Id.ToString)
        End Sub

        Private Sub btnKill_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim objProcess As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessById(intProce ssId)
            objProcess.Kill()
        End Sub
    End Class
End Namespace

Regards,
Karthik Simha

Regards,
Karthik Simha
 
Old November 28th, 2006, 09:17 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 224
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to ashu_from_india Send a message via Yahoo to ashu_from_india
Default

thnx karthik

i hv already tried this...
i hv to start and kill more than 20 processes






Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert a VB.Net app to a web app? furjaw VB.NET 3 September 24th, 2007 12:27 PM
Turning a regular ASP.NET App into an AJAX ASP.Net donrafeal7 Ajax 2 August 31st, 2007 12:33 AM
Is .NET framework req. to install .NET application tact_259 VS.NET 2002/2003 2 May 20th, 2004 08:20 PM
Creating ASP.NET Application in Visual Studio.NET Maxood ASP.NET 1.0 and 1.1 Basics 1 March 8th, 2004 01:56 PM
.NET Remoting application in vb.net Tek4VB .NET Web Services 6 November 19th, 2003 09:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.