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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Killing a process

Killing a process

Scheduled Pinned Locked Moved C#
questioncsharp
17 Posts 4 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.
  • L Lost User

    Why don't you just quit the excel instance through your code?

    Check out the CodeProject forum Guidelines[^]

    C Offline
    C Offline
    CodingYoshi
    wrote on last edited by
    #5

    I tried Quit() along with setting it to null but the process still existed. Any other idea? I am thinking of logging the time my app starts and then checking the start time of each process. If the start time is bigger than start time of app and created by the same user then I will kill it. But it is still possible, the user opened an excel file outside my app while my app is active so this is not the best solution.

    CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

    L 1 Reply Last reply
    0
    • L Luc Pattyn

      Hi, AFAIK there ever is only one Excel process; opening an Excel document causes Excel to start if it isn't already running; opening a second Excel document gets directed towards the first Excel process. So you can only kill one Excel process, and doing so may be a mistake since it may have open documents your app did not open. Your app should tell Excel to close documents, and optionally, when no more documents are open, it could tell Excel to exit. Under no circumstances it should kill Excel. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      C Offline
      C Offline
      CodingYoshi
      wrote on last edited by
      #6

      No, there are actually separate processes created. Excel 2003

      CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

      1 Reply Last reply
      0
      • C CodingYoshi

        I tried Quit() along with setting it to null but the process still existed. Any other idea? I am thinking of logging the time my app starts and then checking the start time of each process. If the start time is bigger than start time of app and created by the same user then I will kill it. But it is still possible, the user opened an excel file outside my app while my app is active so this is not the best solution.

        CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #7

        Did you try something along the lines of Where _excelApplication = Excel.Application

        _excelApplication.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApplication);

        ?

        Check out the CodeProject forum Guidelines[^]

        C 1 Reply Last reply
        0
        • L Lost User

          Did you try something along the lines of Where _excelApplication = Excel.Application

          _excelApplication.Quit();
          System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApplication);

          ?

          Check out the CodeProject forum Guidelines[^]

          C Offline
          C Offline
          CodingYoshi
          wrote on last edited by
          #8

          Just did both quit and ReleaseComObject methods but still there.

          CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

          L 1 Reply Last reply
          0
          • C CodingYoshi

            Just did both quit and ReleaseComObject methods but still there.

            CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #9

            Without seeing your code I'd say your out of luck.

            Check out the CodeProject forum Guidelines[^]

            C 1 Reply Last reply
            0
            • L Lost User

              Without seeing your code I'd say your out of luck.

              Check out the CodeProject forum Guidelines[^]

              C Offline
              C Offline
              CodingYoshi
              wrote on last edited by
              #10

              Hope you can follow it through since I have written it in VB.NET. This is just a wrapper. The parts which might be of interest to you are the constructor and the Create method. Public Class ExcelBook Implements IObservable Private _parameters As Parameters Private _repoHelper As RepositoryHelper Private _sheets As Dictionary(Of String, Worksheet) Private _workBook As Workbook Private _xlApp As Application Private _summarySheet As ExcelSheet Public Sub New(ByVal params As Parameters) Me._xlApp = New Application() Me._workBook = Me._xlApp.Workbooks.Add() Me._repoHelper = Nothing Me._sheets = New Dictionary(Of String, Worksheet)(53) Me._parameters = params End Sub Private Sub DeleteDefaultSheets() Dim DeadSheet As Worksheet DeadSheet = Me._workBook.Sheets("Sheet1") DeadSheet.Delete() DeadSheet = Me._workBook.Sheets("Sheet2") DeadSheet.Delete() DeadSheet = Me._workBook.Sheets("Sheet3") DeadSheet.Delete() End Sub Public Sub Create() Me.CreateRepositories() Dim xSheet As ExcelSheet = Nothing Dim sheetSpecs As Sheet = Nothing Dim wSheet As Worksheet = Nothing ' If the report has a detail sheet and range is multiple for month or week or season then provide detail sheet specs to ' ExcelSheet constructor to create sheets with specification of the detail sheet ' First sheet must be reserved for the summary sheet ' After all detail sheets are finished, a summary sheet must be created. Dim stepInfo As New StepEventArgs() If (Me._parameters.Report.HasDetailSheet And Me._parameters.IsMultipleRanges) Then ' Details Sheets sheetSpecs = Me._parameters.Report.DetailSheet For Each repo As KeyValuePair(Of String, Repository) In Me._repoHelper wSheet = CType(Me._workBook.Worksheets.Add(), Worksheet) wSheet.Name = repo.Key xSheet = New ExcelSheet(wSheet, sheetSpecs, repo.Value, False, Me._parameters, Me._workBook) xSheet.Fill() Me._sheets.Add(repo.Key, xSheet.SpreadSheet) stepInfo.Message = repo.Key & " completed." RaiseEvent StepCompleted(Me, stepInfo) Next Me.DeleteDefaultSheets() ' All detail sheets are done now do the summary Dim repositoryTem

              L 1 Reply Last reply
              0
              • C CodingYoshi

                Hope you can follow it through since I have written it in VB.NET. This is just a wrapper. The parts which might be of interest to you are the constructor and the Create method. Public Class ExcelBook Implements IObservable Private _parameters As Parameters Private _repoHelper As RepositoryHelper Private _sheets As Dictionary(Of String, Worksheet) Private _workBook As Workbook Private _xlApp As Application Private _summarySheet As ExcelSheet Public Sub New(ByVal params As Parameters) Me._xlApp = New Application() Me._workBook = Me._xlApp.Workbooks.Add() Me._repoHelper = Nothing Me._sheets = New Dictionary(Of String, Worksheet)(53) Me._parameters = params End Sub Private Sub DeleteDefaultSheets() Dim DeadSheet As Worksheet DeadSheet = Me._workBook.Sheets("Sheet1") DeadSheet.Delete() DeadSheet = Me._workBook.Sheets("Sheet2") DeadSheet.Delete() DeadSheet = Me._workBook.Sheets("Sheet3") DeadSheet.Delete() End Sub Public Sub Create() Me.CreateRepositories() Dim xSheet As ExcelSheet = Nothing Dim sheetSpecs As Sheet = Nothing Dim wSheet As Worksheet = Nothing ' If the report has a detail sheet and range is multiple for month or week or season then provide detail sheet specs to ' ExcelSheet constructor to create sheets with specification of the detail sheet ' First sheet must be reserved for the summary sheet ' After all detail sheets are finished, a summary sheet must be created. Dim stepInfo As New StepEventArgs() If (Me._parameters.Report.HasDetailSheet And Me._parameters.IsMultipleRanges) Then ' Details Sheets sheetSpecs = Me._parameters.Report.DetailSheet For Each repo As KeyValuePair(Of String, Repository) In Me._repoHelper wSheet = CType(Me._workBook.Worksheets.Add(), Worksheet) wSheet.Name = repo.Key xSheet = New ExcelSheet(wSheet, sheetSpecs, repo.Value, False, Me._parameters, Me._workBook) xSheet.Fill() Me._sheets.Add(repo.Key, xSheet.SpreadSheet) stepInfo.Message = repo.Key & " completed." RaiseEvent StepCompleted(Me, stepInfo) Next Me.DeleteDefaultSheets() ' All detail sheets are done now do the summary Dim repositoryTem

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #11

                next time, post your question in the vb.net forums, it really helps to know what language you are dealing with. Secondly, use the pre tags. Thirdly, this is how I did it (in vb.net)

                Public Sub Terminate()
                If _excelApplication IsNot Nothing Then
                _excelApplication.DisplayAlerts = False
                _excelApplication.Quit()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApplication)
                _excelApplication = Nothing
                _excelApplication.Finalize()
                End If
                End Sub

                jeez.

                Check out the CodeProject forum Guidelines[^]

                C 1 Reply Last reply
                0
                • L Lost User

                  next time, post your question in the vb.net forums, it really helps to know what language you are dealing with. Secondly, use the pre tags. Thirdly, this is how I did it (in vb.net)

                  Public Sub Terminate()
                  If _excelApplication IsNot Nothing Then
                  _excelApplication.DisplayAlerts = False
                  _excelApplication.Quit()
                  System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApplication)
                  _excelApplication = Nothing
                  _excelApplication.Finalize()
                  End If
                  End Sub

                  jeez.

                  Check out the CodeProject forum Guidelines[^]

                  C Offline
                  C Offline
                  CodingYoshi
                  wrote on last edited by
                  #12

                  Sorry about that. The reason I posted it here is because I did not think it will get to the point where I need to post code. Still no luck. The process exists after doing all of the above.

                  EliottA wrote:

                  Thirdly, this is how I did it (in vb.net)

                  You mean it worked for you? Finalize() is not a member of Excel.Application.

                  CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

                  L 1 Reply Last reply
                  0
                  • C CodingYoshi

                    Sorry about that. The reason I posted it here is because I did not think it will get to the point where I need to post code. Still no luck. The process exists after doing all of the above.

                    EliottA wrote:

                    Thirdly, this is how I did it (in vb.net)

                    You mean it worked for you? Finalize() is not a member of Excel.Application.

                    CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #13

                    Sorry, did not mean to throw that line in there.

                    CodingYoshi wrote:

                    he reason I posted it here is because I did not think it will get to the point where I need to post code.

                    Still belongs in the VB.Net forum.

                    CodingYoshi wrote:

                    The process exists after doing all of the above.

                    Err....ummm....does the process exist before execution...?

                    Check out the CodeProject forum Guidelines[^]

                    C 1 Reply Last reply
                    0
                    • L Lost User

                      Sorry, did not mean to throw that line in there.

                      CodingYoshi wrote:

                      he reason I posted it here is because I did not think it will get to the point where I need to post code.

                      Still belongs in the VB.Net forum.

                      CodingYoshi wrote:

                      The process exists after doing all of the above.

                      Err....ummm....does the process exist before execution...?

                      Check out the CodeProject forum Guidelines[^]

                      C Offline
                      C Offline
                      CodingYoshi
                      wrote on last edited by
                      #14

                      No, I manually check to make sure no process exists before the execution and I also kill all the instances programmatically just to reassure using this code: foreach (Process p in Process.GetProcessesByName("EXCEL")) { p.Kill(); }

                      CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

                      L 1 Reply Last reply
                      0
                      • C CodingYoshi

                        No, I manually check to make sure no process exists before the execution and I also kill all the instances programmatically just to reassure using this code: foreach (Process p in Process.GetProcessesByName("EXCEL")) { p.Kill(); }

                        CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #15

                        Again that is horrible design, what if a client has excel running? Create a new instance of excel, and set it's IgnoreRemoteRequests property to true, this will cause *your* instance of excel to operate under a new process.

                        Check out the CodeProject forum Guidelines[^]

                        1 Reply Last reply
                        0
                        • C CodingYoshi

                          I wrote an app which creates excel reports. I would like to kill all the excel processes my application started. How do I figure out if my application started the process so I only kill those processes? I have this code so far: foreach (Process p in Process.GetProcessesByName("EXCEL")) { p.Kill(); } However, as it is clear from the code, it kills all excel processes.

                          CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

                          Y Offline
                          Y Offline
                          yunusdemiray
                          wrote on last edited by
                          #16

                          How do you create excell processes? System.Diagnostics.Process.Start("...") Something like that? If so save the process that you create.

                          List list = new List();
                          Process p = System.Diagnostics.Process.Start("...");
                          list.Add(p);
                          ...
                          list[i].Kill();

                          :)

                          C 1 Reply Last reply
                          0
                          • Y yunusdemiray

                            How do you create excell processes? System.Diagnostics.Process.Start("...") Something like that? If so save the process that you create.

                            List list = new List();
                            Process p = System.Diagnostics.Process.Start("...");
                            list.Add(p);
                            ...
                            list[i].Kill();

                            :)

                            C Offline
                            C Offline
                            CodingYoshi
                            wrote on last edited by
                            #17

                            No, the problem is to make excel spreadsheets you have to create a new instance of the application class and add workbooks to it. This creates a process and I am trying to kill the process which I created but no luck so far. Here is the snippet: public void Constructor(Parameters params) { this._xlApp = New Application(); // This is the process I want to kill once I am finished creating the file. this._workBook = Me._xlApp.Workbooks.Add(); this._repoHelper = null; this._sheets = New Dictionary<String, Worksheet>(53); this._parameters = params; }

                            CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07

                            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