//private Excel.Application XLApp; private void ButtonInvokeAddIn_Click(object sender, RoutedEventArgs e) { // Creates the text file that the trace listener will write to. //FileStream myTraceLog = new FileStream( @"C:\myTraceLog.txt", FileMode.OpenOrCreate ); // Creates the new trace listener. //TextWriterTraceListener myListener = new TextWriterTraceListener( myTraceLog ); try { XLApp = new Excel.Application() { Visible = true }; //Thread.Sleep( 3000 ); XLApp.Workbooks.Add(Excel.XlSheetType.xlWorksheet); Object addInName = "ComServiceOleMarshal"; Office.COMAddIn addIn = XLApp.COMAddIns.Item(ref addInName); while (utils == null) { Thread.Sleep(100); utils = (ComServiceOleMarshal.IAddInUtilities)addIn.Object; } utils.CurrentShowThread(); ComServiceOleMarshal.SomeObject o = new ComServiceOleMarshal.SomeObject(123, "Hello"); utils.DoSomething(o); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { // Flushes the buffers of all listeners in the Listeners collection. //Trace.Flush(); //Flushes only the buffer of myListener. //myListener.Flush(); //myTraceLog.Close(); //And at the end ( 필터링 : reject callee ) //MessageFilter.Revoke(); //if (XLApp != null) //{ // XLApp.Quit(); // XLApp = null; // utils = null; //} //GC.Collect(); //GC.WaitForPendingFinalizers(); //GC.Collect(); //GC.WaitForPendingFinalizers(); } }
private void Un_Unloaded(object sender, RoutedEventArgs e) { if (XLApp != null) { XLApp.Quit(); XLApp = null; utils = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); }