# Visual Basic > Visual Basic .NET > VS 2022 [RESOLVED] Problem in references on System

## sacramento

HI,

Someone Knows why when i add records to excel file the process open i lot of process's in System like in the picture? Any way to resolve the problem?

----------


## jmcilhinney

You need to show us the relevant code. You ALWAYS need to show us the relevant code. How are you opening the Excel files? If it's Interop, are you closing them? How many processes are created each time you open a file? Please provide ALL the relevant information.

----------


## Shaggy Hiker

You almost certainly are not cleaning them up correctly, as that's the main reason for ending up with a whole bunch of Excel processes open. You might also be opening them twice without realizing it, and then, even if you close the one you thought you were opening, you'd still have at least one remaining. 

That's why the code would be necessary.

----------


## sacramento

hi to all,

THis is the code



```
Dim elemValue As String

        excelLocation = My.Application.Info.DirectoryPath & "\Documentos\Formulario_Averbamento.xlsx"

        Dim file As String = excelLocation
        Dim xlApp As excel.Application
        Dim xlWorkBook As excel.Workbook
        Dim xlWorkSheet As excel.Worksheet

        xlApp = New excel.Application
        xlWorkBook = xlApp.Workbooks.Open(file)
        xlWorkSheet = CType(xlWorkBook.Sheets(1), excel.Worksheet)


        'CONTA A PARTIR DA Nº26,COLUNA A, QUE É A PARTIR DAÍ QUE SÃO INSCRITOS OS REGISTOS 
        Dim col As String = "A"
        For row As Integer = 26 To xlWorkSheet.UsedRange.Rows.Count - 1
            elemValue = xlWorkSheet.Range(col & row).Text
            If elemValue = "" Then
                Exit For
            End If
            var_contar_sheets = var_contar_sheets + 1
        Next

        xlWorkBook.Close()
        xlApp.Quit()
        xlApp = Nothing
```

----------


## .paul.

Apparently, adding these four lines after your code there will clean up any unused processes...



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

----------


## sacramento

> Apparently, adding these four lines after your code there will clean up any unused processes...
> 
> 
> 
> ```
> GC.Collect()
> GC.WaitForPendingFinalizers()
> GC.Collect()
> GC.WaitForPendingFinalizers()
> ```



OK...Work...Thanks

----------


## TysonLPrice

Here is an example I use:



```
Imports Microsoft.Office.Interop

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        Dim xl As Object
        Dim xlWorkBooks As Object
        Dim xlWorkBook As Object
        Dim xlWorksheet As Object
        xl = CreateObject("Excel.Application")

        xl.DisplayAlerts = False
        xlWorkBooks = xl.Workbooks
        xlWorkBook = xl.Workbooks.Open("C:\TestIt.xlsx")
        xlWorksheet = xl.Sheets(1)
        xlWorksheet.Activate()

        xlWorksheet.Cells(1, 2).Value = "XX"

        xl.Application.ActiveWorkbook.SaveAs("C:\TestItOut.xlsx")


        xlWorkBook.Close()
        xlWorksheet = Nothing
        xl.Quit()
        xl = Nothing

        ReleaseComObject(xlWorkBook)
        ReleaseComObject(xlWorkBooks)
        ReleaseComObject(xl)

        GC.Collect()

        MessageBox.Show("Done")

    End Sub

    Private Sub ReleaseComObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
            Exit Sub
        Catch ex As Exception
            obj = Nothing
        End Try
    End Sub

End Class
```

----------


## jmcilhinney

> Here is an example I use:
> 
> 
> 
> ```
> Imports Microsoft.Office.Interop
> 
> Public Class Form1
> 
> ...


That code appears to have some flaws in it. Firstly, while it won't hurt, there's little point to setting _obj_ to _Nothing_ in that _ReleaseComObject_ method as it's a reference type passed by value. That assignment has no effect on the variable passed as an argument. Secondly, you're doing this:

vb.net Code:
xl = Nothing
 '...
 ReleaseComObject(xl)
so you're not releasing any COM object there. Also, there's little point to setting any variable to _Nothing_ in event handler either. They're all local variables so they fall out of scope once the method completes anyway. If they were fields, then that would serve a purpose.

----------


## TysonLPrice

> That code appears to have some flaws in it. Firstly, while it won't hurt, there's little point to setting _obj_ to _Nothing_ in that _ReleaseComObject_ method as it's a reference type passed by value. That assignment has no effect on the variable passed as an argument. Secondly, you're doing this:
> 
> vb.net Code:
> xl = Nothing
>  '...
>  ReleaseComObject(xl)
> so you're not releasing any COM object there. Also, there's little point to setting any variable to _Nothing_ in event handler either. They're all local variables so they fall out of scope once the method completes anyway. If they were fields, then that would serve a purpose.


Thanks...I'll clean up my example and then I'll see if I can find where I got that and let them know  :wave:

----------


## Shaggy Hiker

My guess is that you got it from somebody more familiar with VB6 that .NET. Setting variables to Nothing is frequently done in that language, and selectively useful in .NET. It does no harm in either language, it just isn't useful quite as often in .NET. Therefore, if your habits formed in VB6, there is a good chance that they will carry over into .NET, as the normal de-conditioning won't happen.

----------

