如何杀死Excel

时间:2012-03-19 14:12:34

标签: c# winforms multithreading excel timer

我在控制台应用X中有一个c#方法,这启动了一个进程;控制台应用Y(用相同的c#解决方案编写)。 App Y然后在Excel 2010工作簿中触发vba宏。

为了在wkbook VBA中进行测试,我添加了一些代码来强制运行时错误1004。

winForm使用一个使用Forms计时器触发的进程事件来终止进程。它按照程序运行我只是想尝试让它做更多。 为什么,当我杀死进程时,XL的实例是在发现错误时保持打开状态?我如何找到摆脱XL实例的方法,如果它仍然存在,何时杀死进程,然后将错误消息发回给我的winForm?

(ps以下代码很熟悉,但问题不重复)

    private int elapsedTime;
    private Process p;
    private System.Windows.Forms.Timer myTimer;
    const int SLEEP_AMOUNT = 1000;//1s
    const int MAXIMUM_EXECUTION_TIME = 5000;//5s


    private void btRunReport_Click(object sender, EventArgs e) {
        btRunReport.Enabled = false;
        lbStatusUpdate.Text = "Processing..";

        //instantiate a new process and set up an event for when it exits
        p = new Process();
        p.Exited += new EventHandler(MyProcessExited);
        p.EnableRaisingEvents = true;
        p.SynchronizingObject = this;
        elapsedTime = 0;
        this.RunReportScheduler();

        //add in a forms timer so that the process can be killed after a certain amount of time
        myTimer = new System.Windows.Forms.Timer();
        myTimer.Interval = SLEEP_AMOUNT;
        myTimer.Tick += new EventHandler(TimerTickEvent);
        myTimer.Start();

    }
    private void RunReportScheduler() {
        p.StartInfo.FileName = @"\\fileserve\department$\ReportScheduler_v3.exe";
        p.StartInfo.Arguments = 2;
        p.Start();
    }
    private void MyProcessExited(Object source, EventArgs e){
        myTimer.Stop();
        btRunReport.Enabled = true;
        lbStatusUpdate.Text = "Start";
    }
    void TimerTickEvent(Object myObject, EventArgs myEventArgs) {
        myTimer.Stop();
        elapsedTime += SLEEP_AMOUNT;
        if (elapsedTime > MAXIMUM_EXECUTION_TIME)
        {p.Kill();}
        else
        {myTimer.Start();}
    }

2 个答案:

答案 0 :(得分:1)

它可能是报告调度程序的问题,它没有关闭Excel的正确方法。

这是这样的方法:

private void releaseObject(object obj)
{
    try
    {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
    }
    catch (Exception ex)
    {
        obj = null;
        MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
    }
    finally
    {
        GC.Collect();
    }
}

答案 1 :(得分:0)

我原来的代码没有改变,但是我使用了安德鲁的帮助,但主要是我的一位好朋友的帮助,遗憾的是他没有签约。 Excel好像死了!另外,他以这样一种方式对其进行编码,即它传回一个指示器,告诉表单是否存在excel问题。同时为我们提供了为每个Excel流程构建最大运行时间的选项。

他使用以下SO answer来帮助摆脱Excel

1.在日程安排程序中

  • 移动计时器
  • 在vba中没有错误的情况下实施excel清洁代码,在相反的情况下实现最大执行时间(使用Kill方法)
  • 如果excel正常结束,则从调度程序返回0到表单应用程序,如果已被终止,则返回1

2.在表单应用程序中,分析ProcessExited事件处理程序和启用按钮等中调度程序的返回值

所以,新的调度程序:

 using System;
 using System.Text;
 using System.Runtime.InteropServices;
 using System.Diagnostics;
 using Excel = Microsoft.Office.Interop.Excel;
 using System.Timers;


class Program
{
   private const int SLEEP_AMOUNT = 1000;
   private const int MAXIMUM_EXECUTION_TIME = 10000;
   private Excel.Application excelApp =null;
   private Excel.Workbook book =null;
   private Timer myTimer;
   private int elapsedTime;
   private int exitCode=0;

   [DllImport("user32.dll", SetLastError =true)]
   static extern uint GetWindowThreadProcessId(IntPtr hWnd,out uint lpdwProcessId);

   static int Main(string[] args)
    {
       Program myProgram = newProgram();
       myProgram.RunExcelReporting(1);
       return myProgram.exitCode;
    }


   void myTimer_Elapsed(object sender,ElapsedEventArgs e)
    {
       myTimer.Stop();
       elapsedTime += SLEEP_AMOUNT;
       if (elapsedTime > MAXIMUM_EXECUTION_TIME)
        {
            //error in vba or maximum time reached. abort excel and return 1 to the calling windows forms application
           GC.Collect();
           GC.WaitForPendingFinalizers();
           if (book != null)
            {
               book.Close(false,Type.Missing, Type.Missing);
               Marshal.FinalReleaseComObject(book);
               book =null;
            }

           if (excelApp != null)
            {
               int hWnd = excelApp.Hwnd;
               uint processID;
               GetWindowThreadProcessId((IntPtr)hWnd,out processID);
               if (processID != 0)
                   Process.GetProcessById((int)processID).Kill();
                excelApp =null;
                exitCode = 1;
            }
        }
       else
        {
            myTimer.Start();
        }
    }


   void RunExcelReporting(int x)
    {
        myTimer =new Timer(SLEEP_AMOUNT);
        elapsedTime = 0;
        myTimer.Elapsed +=new ElapsedEventHandler(myTimer_Elapsed);
        myTimer.Start();

       try{
            excelApp =new Excel.Application();
            excelApp.Visible =true;
            book = excelApp.Workbooks.Open(@"c:\jsauto.xlsm");
            excelApp.Run("ThisWorkbook.rr");
            book.Close(false,Type.Missing, Type.Missing);
        }
        catch (Exception ex){
           Console.WriteLine(ex.ToString());
        }

       finally
        {
           //no error in vba and maximum time is not reached. clear excel normally
           GC.Collect();
           GC.WaitForPendingFinalizers();

           if (book != null)
            {
               try {
                    book.Close(false,Type.Missing, Type.Missing);
                }
                catch { }
               Marshal.FinalReleaseComObject(book);
            }

           if (excelApp != null)
            {
               excelApp.Quit();
               Marshal.FinalReleaseComObject(excelApp);
               excelApp =null;
            }
        }
    }
}

新表格申请:

public partial class Form1 : Form

{
   SqlDataAdapter myAdapt = null; 
   DataSet mySet =null; 
   DataTable myTable =null; 

   public Form1()
    { InitializeComponent();}

    privatevoid Form1_Load(object sender,EventArgs e){ 
        InitializeGridView();
    }

   private Process myProcess;

   private void btRunProcessAndRefresh_Click(object sender,EventArgs e)
    {
        myProcess =new Process();
        myProcess.StartInfo.FileName =@"c:\VS2010Projects\ConsoleApplication2\ConsoleApplication4\bin\Debug\ConsoleApplication4.exe";
        myProcess.Exited +=new EventHandler(MyProcessExited);
        myProcess.EnableRaisingEvents =true;
        myProcess.SynchronizingObject =this;
        btRunProcessAndRefresh.Enabled =false;
        myProcess.Start();
    }

    privatevoid MyProcessExited(Object source,EventArgs e)
    {
        InitializeGridView();
        btRunProcessAndRefresh.Enabled =true;
       if (((Process)source).ExitCode == 1)
        {
           MessageBox.Show("Excel was aborted");
        }
       else
        {
           MessageBox.Show("Excel finished normally");
        }
    }

   private void btnALWAYSWORKS_Click(object sender,EventArgs e) { 
        InitializeGridView();
    }

    privatevoid InitializeGridView() { 
      using (SqlConnection conn =new SqlConnection(@"Data Source=sqliom3;Integrated Security=SSPI;Initial Catalog=CCL"))
        {
        myAdapt =new SqlDataAdapter("SELECT convert(varchar(25),getdate(),120) CurrentDate", conn);
        mySet =new DataSet();
        myAdapt.Fill(mySet,"AvailableValues"); 
        myTable = mySet.Tables["AvailableValues"];

        this.dataGridViewControlTable.DataSource = myTable;
        this.dataGridViewControlTable.AllowUserToOrderColumns =true;
        this.dataGridViewControlTable.Refresh();
        }
    }
  }