.net - Quit excel instance as class member on demand -
I try to create a square where I want to work with an Excel instance and I demand that drop On the example excellence (want to be outlined in still want to be able to differentiate the following ways in the garbage colletion:. Overview The task manager is loaded as a form I am not satisfied with the class. Ause I make the personal variable of Excel no longer closes on demand. Doese anyone know (why does the snippet work according to my needs and not the other?
Excel Import excel = Microsoft.Office.Interop.Excel Folk Class Form1 Personal sub Form1_Load (as the object, E EventArgs) handles MyBase.Load dim xlsInst New ExcelInst GC.C As ollect () GC.WaitForPendingFinalizers () End Sub ending class folk classroom ExcelInst sub new () Dim myxlApp New Excel.Application dim myxlWb Excel.Workbook as' ~~ & gt; a new workbook as myxlWb = myxlApp .Workbooks.Add '~~ & gt; Display Excel myxlApp.Visible = true myxlApp.Quit () End Sub Ending Class myxlApp and myxlWB Want to But if I change my class to
public class ExcelInst Personal MyxlApp New Excel.Application as personal myxlWb Excel.Workbook sub New () '~~ & gt; Add a new workbook to myxlWb = myxlApp.Workbooks.Add '~~ & gt; Display Excel myxlApp.Visible = true myxlApp.Quit () End Sub End Class
I will assume that you still have a reference to xlsInst before the GC, compile and that object contains the references of Excel, GC will not collect. XlsInst = Do not try to do anything before collecting, And maybe not set the variable to anything in the class.
< Pre> Private sub form 1_load (object as sender, and sender in event as ARG) MyBase handles. New Exceline xlsInst = Add as some GC compiler () GC VATFORDFiNIEZ () and sub end class Public class ExcelInst Personal MyxlApp as new Excel application as private myxlWb Excel. Workbook sub new () '~~ & gt; Add a new workbook myxlWb = myxlApp.Workbooks.Add' ~~> Excel myxlApp displays Please .Visible = True myxlApp.Quit () Myxl App = None MyxlWb = Nothing End Sub End Class I have to add, I find it strange that the argument should be within the new (). I suggest keeping it in a steady job or like a processing axle () or .closeExcel () function.
Comments
Post a Comment