0

I am working on creating an excel report in VB.Net. The report created successfully and i killed the processes at the end. But when the same report runs simultaneously from different machines i.e it is used by multiple users, it kills all the processes and only one user is able to create the report.

Can you please suggests how can i close only the specific excel process once it is created. Below is my code

If Not _xlApp.Workbooks Is Nothing Then
        For Each wb In _xlApp.Workbooks
            wb.Close(False)
        Next
        _xlApp.Workbooks.Close()
    End If
    _xlApp.DisplayAlerts = False
    _xlApp.Quit()

    GC.Collect()
    GC.WaitForPendingFinalizers()
    GC.Collect()
    GC.WaitForPendingFinalizers()


    If _oSheet IsNot Nothing Then
        Runtime.InteropServices.Marshal.ReleaseComObject(_oSheet)
    End If



    Runtime.InteropServices.Marshal.ReleaseComObject(_oBook)

    _xlApp.Quit()
    Runtime.InteropServices.Marshal.ReleaseComObject(_xlApp)


    GC.Collect()
    GC.WaitForPendingFinalizers()
    GC.Collect()
    GC.WaitForPendingFinalizers()
    Dim proc As Diagnostics.Process

    For Each proc In Diagnostics.Process.GetProcessesByName("EXCEL")
        Try
            proc.Kill()
        Catch ex As Exception

        End Try

    Next

The problem is that i cannot identify the PID of the processes that should be closed when an excel is created.

Abdul
  • 2,002
  • 7
  • 31
  • 65

1 Answers1

0

You shouldn't need to kill the process of manually like that. It does take a while for the garbage collection to come get the COM Object. Mine tends to not remove the object until I back out of the form and most of the application. Here's a few links that may help.

Release Excel COM Objects

Why doesn't Excel Quit

Understanding Garbage Collection in .NET

Daffas
  • 87
  • 7