0

私は C# & VS2013 と VSTO を使用しています。私のコードは完璧に実行されますが、1 つの問題は、タスク マネージャーに移動して [詳細] を見ると、その後も Excel が実行中であると表示され、適切にクリーンアップされたように感じられることです。

これは、Excel へのすべての参照をクリーンアップして閉じるために使用する構文です。Excel と VSTO への言及だけで、脚の仕事をする肉と肉汁を省略しました。そうしないと、この投稿が長くなりすぎます。

何か設定が間違っていませんか?タスク マネージャーに Excel がまだ表示されるのはなぜですか?

namespace ReleaseExcel
{
  public partial class Form12 : Form12
  {
    public static Excel.Worksheet newSeet;
    public static Excel._Worksheet currentsheet;
    public static Excel._Workbook oWB;
    public static Excel.Application xlApp;
    public static Excel.Workbook xlWorkBook;
    public static Excel.Worksheet xlWorkSheet;
    public static Excel.Workbook activeworkbook;

    public Form1()
    {
      InitializeComponents();
    }
    private void DoThis()
    {
        //Showing Declerations to Excel
        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        //Do some more stuff here

        //Showing a few more Declerations to Excel
        currentsheet = (Excel._Worksheet)(xlWorkBook.ActiveSheet);
        Excel.Range last = currentsheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);

        //Do a bit more stuff
        ClearExcel();
        while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet) != 0) { }
        while (System.Runtime.InteropServices.Marshal.ReleaseComObject(currentsheet) != 0) { }
        while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook) != 0) { }
        while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) != 0) { }
    }
    private static void ClearExcel()
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }      
  }
}

編集&
に追加しましたが、構文がハングアップしますGC.Collect()GC.WaitForPendingFinalizers()

while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook) != 0) { }

約 5 分間この行に表示されていますが、フリーズの原因は何ですか?

編集#2
まだそのような運はありません。これはコードです

namespace ReleaseExcel
{
   public partial class Form12 : Form12
  {
    public static Excel.Worksheet newSeet;
    public static Excel._Worksheet currentsheet;
    public static Excel._Workbook oWB;
    public static Excel.Application xlApp;
    public static Excel.Workbook xlWorkBook;
    public static Excel.Worksheet xlWorkSheet;
    public static Excel.Workbook activeworkbook;

    public Form1()
    {
      InitializeComponents();
    }

    public void btnPush_Click(object sender, EventArgs e)
    {
        DoThis();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    private void DoThis()
    {
      //Showing Declerations to Excel
      xlApp = new Excel.Application();
      xlWorkBook = xlApp.Workbooks.Add(misValue);
      xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

      //Do some more stuff here

      //Showing a few more Declerations to Excel
      currentsheet = (Excel._Worksheet)(xlWorkBook.ActiveSheet);
      Excel.Range last = currentsheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);

    }
  }
}
4

0 に答える 0