How to Kill Excel Task

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph

    #1

    How to Kill Excel Task

    Hello,
    I am developing a program that will read data from a
    couple of text files, process it, and then, populate data
    into specific cells in an Excel spreadsheet.

    Towards the end of the program, I try to exit from Excel
    using the followings statements.

    m_objBook.Close (false, m_objOpt, m_objOpt);
    m_objExcel.Quit ();

    (Definitions are as follows...
    Excel._Workbook m_objBook
    object m_objOpt = System.Reflecti on.Missing.Valu e;
    Excel.Applicati on m_objExcel
    )

    The program ends successfully, but after I execute it a
    couple of times, Excel stops responding when I try to
    open the resultant file. When I go to task manager, I can
    see several Excel.exe tasks out there (even if I don't
    have any Excel file open) and I have to close off those
    tasks before I can open any excel file.

    Any suggestions on how to resolve this issue?
    Any help would be greatly appreciated.

    thanks!

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: How to Kill Excel Task

    Joseph,

    You will have to post more of your code. More likely than not, you are
    not releasing every reference to an object in Excel. Basically whenever you
    access a property, you are increasing the reference count of the main
    application (since almost every object in Excel has a reference to
    Application). Because of this, every time you get a return value from a
    property or method which is an Excel object, you have to make sure you
    decrement the reference count (usually a call to the static ReleaseComObjec t
    method on the Marshal class).

    If you have so much code that you can't check for this everywhere, then
    you will want to make sure that all the managed references are null. Once
    they are and you call Quit, you should be able to call the static Collect
    method on the GC class and then the Excel instance will go away.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Joseph" <jose24john24@n ospam.com> wrote in message
    news:082901c377 b8$1a02e130$a00 1280a@phx.gbl.. .[color=blue]
    > Hello,
    > I am developing a program that will read data from a
    > couple of text files, process it, and then, populate data
    > into specific cells in an Excel spreadsheet.
    >
    > Towards the end of the program, I try to exit from Excel
    > using the followings statements.
    >
    > m_objBook.Close (false, m_objOpt, m_objOpt);
    > m_objExcel.Quit ();
    >
    > (Definitions are as follows...
    > Excel._Workbook m_objBook
    > object m_objOpt = System.Reflecti on.Missing.Valu e;
    > Excel.Applicati on m_objExcel
    > )
    >
    > The program ends successfully, but after I execute it a
    > couple of times, Excel stops responding when I try to
    > open the resultant file. When I go to task manager, I can
    > see several Excel.exe tasks out there (even if I don't
    > have any Excel file open) and I have to close off those
    > tasks before I can open any excel file.
    >
    > Any suggestions on how to resolve this issue?
    > Any help would be greatly appreciated.
    >
    > thanks!
    >[/color]


    Comment

    • Joseph

      #3
      Re: How to Kill Excel Task

      Nicholas,
      thank you for your suggestion. You are right about me
      not releasing every (any) reference to the Excel objects.
      I am fairly new to this platform and so, didn't know that
      I had to release the references. I tried coding the
      GC.collect(0) before the quit and the problem seems to
      have been resolved.
      One thing that I am not clear about is why this problem
      was occurring at random (earlier)... It was not a
      consistant problem - but I guess that must be because GC
      must have been invoked by the system sometimes...
      Is that correct?

      thanks again![color=blue]
      >-----Original Message-----
      >Joseph,
      >
      > You will have to post more of your code. More likely[/color]
      than not, you are[color=blue]
      >not releasing every reference to an object in Excel.[/color]
      Basically whenever you[color=blue]
      >access a property, you are increasing the reference count[/color]
      of the main[color=blue]
      >application (since almost every object in Excel has a[/color]
      reference to[color=blue]
      >Application) . Because of this, every time you get a[/color]
      return value from a[color=blue]
      >property or method which is an Excel object, you have to[/color]
      make sure you[color=blue]
      >decrement the reference count (usually a call to the[/color]
      static ReleaseComObjec t[color=blue]
      >method on the Marshal class).
      >
      > If you have so much code that you can't check for[/color]
      this everywhere, then[color=blue]
      >you will want to make sure that all the managed[/color]
      references are null. Once[color=blue]
      >they are and you call Quit, you should be able to call[/color]
      the static Collect[color=blue]
      >method on the GC class and then the Excel instance will[/color]
      go away.[color=blue]
      >
      > Hope this helps.
      >
      >
      >--
      > - Nicholas Paldino [.NET/C# MVP]
      > - nicholas.paldin o@exisconsultin g.com
      >
      >"Joseph" <jose24john24@n ospam.com> wrote in message
      >news:082901c37 7b8$1a02e130$a0 01280a@phx.gbl. ..[color=green]
      >> Hello,
      >> I am developing a program that will read data from a
      >> couple of text files, process it, and then, populate[/color][/color]
      data[color=blue][color=green]
      >> into specific cells in an Excel spreadsheet.
      >>
      >> Towards the end of the program, I try to exit from Excel
      >> using the followings statements.
      >>
      >> m_objBook.Close (false, m_objOpt, m_objOpt);
      >> m_objExcel.Quit ();
      >>
      >> (Definitions are as follows...
      >> Excel._Workbook m_objBook
      >> object m_objOpt = System.Reflecti on.Missing.Valu e;
      >> Excel.Applicati on m_objExcel
      >> )
      >>
      >> The program ends successfully, but after I execute it a
      >> couple of times, Excel stops responding when I try to
      >> open the resultant file. When I go to task manager, I[/color][/color]
      can[color=blue][color=green]
      >> see several Excel.exe tasks out there (even if I don't
      >> have any Excel file open) and I have to close off those
      >> tasks before I can open any excel file.
      >>
      >> Any suggestions on how to resolve this issue?
      >> Any help would be greatly appreciated.
      >>
      >> thanks!
      >>[/color]
      >
      >
      >.
      >[/color]

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: How to Kill Excel Task

        Joseph,

        It is possible that the GC was being called by the system, but also,
        because I don't know how you were accessing the Excel object model, I don't
        know what you were cleaning up (or not) or how often.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - nicholas.paldin o@exisconsultin g.com

        "Joseph" <jose24john24@n ospam.com> wrote in message
        news:679801c377 cb$ae3919b0$a50 1280a@phx.gbl.. .[color=blue]
        > Nicholas,
        > thank you for your suggestion. You are right about me
        > not releasing every (any) reference to the Excel objects.
        > I am fairly new to this platform and so, didn't know that
        > I had to release the references. I tried coding the
        > GC.collect(0) before the quit and the problem seems to
        > have been resolved.
        > One thing that I am not clear about is why this problem
        > was occurring at random (earlier)... It was not a
        > consistant problem - but I guess that must be because GC
        > must have been invoked by the system sometimes...
        > Is that correct?
        >
        > thanks again![color=green]
        > >-----Original Message-----
        > >Joseph,
        > >
        > > You will have to post more of your code. More likely[/color]
        > than not, you are[color=green]
        > >not releasing every reference to an object in Excel.[/color]
        > Basically whenever you[color=green]
        > >access a property, you are increasing the reference count[/color]
        > of the main[color=green]
        > >application (since almost every object in Excel has a[/color]
        > reference to[color=green]
        > >Application) . Because of this, every time you get a[/color]
        > return value from a[color=green]
        > >property or method which is an Excel object, you have to[/color]
        > make sure you[color=green]
        > >decrement the reference count (usually a call to the[/color]
        > static ReleaseComObjec t[color=green]
        > >method on the Marshal class).
        > >
        > > If you have so much code that you can't check for[/color]
        > this everywhere, then[color=green]
        > >you will want to make sure that all the managed[/color]
        > references are null. Once[color=green]
        > >they are and you call Quit, you should be able to call[/color]
        > the static Collect[color=green]
        > >method on the GC class and then the Excel instance will[/color]
        > go away.[color=green]
        > >
        > > Hope this helps.
        > >
        > >
        > >--
        > > - Nicholas Paldino [.NET/C# MVP]
        > > - nicholas.paldin o@exisconsultin g.com
        > >
        > >"Joseph" <jose24john24@n ospam.com> wrote in message
        > >news:082901c37 7b8$1a02e130$a0 01280a@phx.gbl. ..[color=darkred]
        > >> Hello,
        > >> I am developing a program that will read data from a
        > >> couple of text files, process it, and then, populate[/color][/color]
        > data[color=green][color=darkred]
        > >> into specific cells in an Excel spreadsheet.
        > >>
        > >> Towards the end of the program, I try to exit from Excel
        > >> using the followings statements.
        > >>
        > >> m_objBook.Close (false, m_objOpt, m_objOpt);
        > >> m_objExcel.Quit ();
        > >>
        > >> (Definitions are as follows...
        > >> Excel._Workbook m_objBook
        > >> object m_objOpt = System.Reflecti on.Missing.Valu e;
        > >> Excel.Applicati on m_objExcel
        > >> )
        > >>
        > >> The program ends successfully, but after I execute it a
        > >> couple of times, Excel stops responding when I try to
        > >> open the resultant file. When I go to task manager, I[/color][/color]
        > can[color=green][color=darkred]
        > >> see several Excel.exe tasks out there (even if I don't
        > >> have any Excel file open) and I have to close off those
        > >> tasks before I can open any excel file.
        > >>
        > >> Any suggestions on how to resolve this issue?
        > >> Any help would be greatly appreciated.
        > >>
        > >> thanks!
        > >>[/color]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        Working...