Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ReleaseComObject() or not?

ReleaseComObject() or not?

Scheduled Pinned Locked Moved C#
questioncomtutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    __John_
    wrote on last edited by
    #1

    How do I know whether or not to call ReleaseComObject()? For example in the following code I call ReleaseComObject() on the Worksheet, it works fine, but I dont rearly know if this is correct or not. How would I know? Is there a rule that I need to follow?

    try
    {

    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
    
    string strWorkBook = @"C:\\zz\\Book1.xlsx";
    
    Workbook workBook = excelApp.Workbooks.Open(strWorkBook,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);
    
    
    int nSheets = workBook.Sheets.Count;
                
    Worksheet sheet = (Worksheet)workBook.Sheets\[1\];
    
    
    // Do somthing with the Worksheet...
    
    
    Marshal.ReleaseComObject(sheet);
    
    workBook.Close(false, strWorkBook, null);
    Marshal.ReleaseComObject(workBook);
    

    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

    “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

    A 1 Reply Last reply
    0
    • _ __John_

      How do I know whether or not to call ReleaseComObject()? For example in the following code I call ReleaseComObject() on the Worksheet, it works fine, but I dont rearly know if this is correct or not. How would I know? Is there a rule that I need to follow?

      try
      {

      Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
      
      string strWorkBook = @"C:\\zz\\Book1.xlsx";
      
      Workbook workBook = excelApp.Workbooks.Open(strWorkBook,
              Type.Missing, Type.Missing, Type.Missing, Type.Missing,
              Type.Missing, Type.Missing, Type.Missing, Type.Missing,
              Type.Missing, Type.Missing, Type.Missing, Type.Missing,
              Type.Missing, Type.Missing);
      
      
      int nSheets = workBook.Sheets.Count;
                  
      Worksheet sheet = (Worksheet)workBook.Sheets\[1\];
      
      
      // Do somthing with the Worksheet...
      
      
      Marshal.ReleaseComObject(sheet);
      
      workBook.Close(false, strWorkBook, null);
      Marshal.ReleaseComObject(workBook);
      

      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }

      “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      As a rule, its advisable to release com objects from managed code.

      P 1 Reply Last reply
      0
      • A Abhinav S

        As a rule, its advisable to release com objects from managed code.

        P Offline
        P Offline
        pietvredeveld
        wrote on last edited by
        #3

        Don't agree to that. ReleaseComObject should only be called when absolutely required. Even then it's better to call FinalReleaseComObject. See http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx[^] Piet

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups