c# - Cannot close Excel.exe after killing it from Task Manager -


i using visual studio 2015 community in c#, , office 2013 standalone.

the below code worked open , close excel. added code open specific worksheet in example 2 below. and, worked, excel closed correctly.

then moved things around forgot use releasecomobject worksheet , excel stayed open, manually closed in task manager.

now, none of below examples work. after accidentally forgot release com object, excel never closes, unless reboot machine.

example 1

private bool loadexcel() {     // open excel     excel.application myapp = new excel.application();     // hide excel     myapp.visible = false;     gsiworkbook = myapp.workbooks.open( "d:\\test.xlsx" );     // cleanup     gsiworkbook.close( false );     // manual disposal because of com     while ( system.runtime.interopservices.marshal.releasecomobject( gsiworkbook ) != 0 )     { }      myapp.application.quit();     myapp.quit();     while ( system.runtime.interopservices.marshal.releasecomobject( myapp ) != 0 )     { }      myapp = null;     gsiworkbook = null;      gc.collect();     gc.waitforpendingfinalizers();     return true;  } // end loadexcel 

example 2

private bool loadexcel() {     // open excel     excel.application myapp = new excel.application();     // hide excel     myapp.visible = false;     gsiworkbook = myapp.workbooks.open( "d:\\test.xlsx" );      ( int counter = 1; counter < 100; counter++ )     {         try         {             gsiworksheet = gsiworkbook.sheets[counter];             if ( gsiworksheet.name == _gsi2sheet )                 break;         }         catch         {             continue;         }     }      while ( system.runtime.interopservices.marshal.releasecomobject( gsiworksheet ) != 0 )     { }      // cleanup     gsiworkbook.close( false );     // manual disposal because of com     while ( system.runtime.interopservices.marshal.releasecomobject( gsiworkbook ) != 0 )     { }      myapp.application.quit();     myapp.quit();     while ( system.runtime.interopservices.marshal.releasecomobject( myapp ) != 0 )     { }      myapp = null;     gsiworkbook = null;      gc.collect();     gc.waitforpendingfinalizers();     return true; } // end loadexcel 

if show complete usage of excel object, bet find breaking 2 dot rule. sounds absurd know, think inadvertently creating instances of objects cannot disposed excel or gc , cannot close properly. (called runtime callable wrappers)

when use - - code:

xlworkbook = xlapp.workbooks.add 

...you causing problem. need create variable pointing workbooks , use directly , dispose of it. example link:

dim xlapp new excel.application dim xlworkbooks excel.workbooks = xlapp.workbooks dim xlworkbook excel.workbook = xlworkbooks.add() 

....all code, then

xlapp.quit()  if not xlworkbook nothing     marshal.finalreleasecomobject (xlworkbook)     xlworkbook = nothing end if if not xlworkbooks nothing     marshal.finalreleasecomobject (xlworkbooks)     xlworkbooks = nothing end if if not xlapp nothing     marshal.finalreleasecomobject (xlapp)     xlapp = nothing end if  xlapp.quit() 

see here details:

http://www.siddharthrout.com/2012/08/06/vb-net-two-dot-rule-when-working-with-office-applications-2/


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -