2007
05.28

On 18th May 2007, Tanmoy commented on “How To Kill MS Excel Process Started by ASP.NET”.

Initially on denial (^_^)/, I finally have time to investigate the issue. Tanmoy actually got it correctly. So I created a new GUI demo on how to correctly kill a particular Excel process.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel;
using System.Diagnostics;


namespace ThreadingGUI_01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ApplicationClass app = new ApplicationClass();
            if (app == null)
            {
                MessageBox.Show("Unable to Open Excel");
            }
           
            app.Visible = true;
            _Workbook wb = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            this.listBox1.Items.Add(app.Hwnd);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex >= 0)
            {
                Process[] procs = Process.GetProcessesByName("EXCEL");
                int selectedExcel = (Int32)this.listBox1.SelectedItem;
                foreach (Process p in procs)
                {                    
                    if (p.MainWindowHandle.ToInt32() == selectedExcel )
                    {
                        p.Kill();
                        this.listBox1.Items.Remove(selectedExcel);
                    }
                }
            }
        }
    }
}

Thanks to Tanmoy (^_^)/

Download the Project here.

GD Star Rating
a WordPress rating system
Worth sharing ?

Incoming Search Term

Advertise Here

1 Trackback

 

 

No Comment

Add Your Comment