Home > Software engineering >  What does VB.NET do differently than C# that Excel exits after the console-app ends without releasin
What does VB.NET do differently than C# that Excel exits after the console-app ends without releasin

Time:10-24

I've got two .net-6 console-apps:

one in C#

using Excel = Microsoft.Office.Interop.Excel;

Console.WriteLine("Hello, World!");

var xl = new Excel.Application();
var wb = xl.Workbooks.Open(@"c:\temp\test-2.xlsx");

wb.Close(0);
xl.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl);

wb = null;
xl = null;

the other one in VB.NET.

Imports System
Imports Excel = Microsoft.Office.Interop.Excel

Module Program
    Sub Main(args As String())
        Console.WriteLine("Hello World!")

        Dim xl = New Excel.Application()
        Dim wb = xl.Workbooks.Open("c:\\temp\\test-2.xlsx")

        wb.Close(0)
        xl.Quit()

        'System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
        'System.Runtime.InteropServices.Marshal.ReleaseComObject(xl)

        'wb = Nothing
        'xl = Nothing


    End Sub
End Module

The project configuration is in both consoles is the same:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <RootNamespace>OfficeInteropTest</RootNamespace>        
    </PropertyGroup>

    <ItemGroup>
        <COMReference Include="Microsoft.Office.Interop.Excel">
            <WrapperTool>tlbimp</WrapperTool>
            <VersionMinor>9</VersionMinor>
            <VersionMajor>1</VersionMajor>
            <Guid>00020813-0000-0000-c000-000000000046</Guid>
            <Lcid>0</Lcid>
            <Isolated>false</Isolated>
            <EmbedInteropTypes>true</EmbedInteropTypes>
        </COMReference>
    </ItemGroup>

</Project>

When I run each of them the C# app does not close Excel after the console exits while the VB.NET app does even though I commented out the last four lines.

So, I was wondering what do they do differently and why does it look like VB.NET is better with Office interop?

CodePudding user response:

It turns out that Dai's susspicion was correct. VB.NET adds STAThread implicitly. I've looked into the ILCode and there it is:

.method public static void
    Main(
      string[] args
    ) cil managed
  {
    .entrypoint
    .custom instance void [System.Runtime]System.STAThreadAttribute::.ctor()
      = (01 00 00 00 )
    .maxstack 16
    .locals init (
      [0] class Microsoft.Office.Interop.Excel.Application xl,
      [1] class Microsoft.Office.Interop.Excel.Workbook wb
    )

    // [5 5 - 5 31]
    IL_0000: nop

    // [6 9 - 6 42]
    IL_0001: ldstr        "Hello World!"
    IL_0006: call         void [System.Console]System.Console::WriteLine(string)
    IL_000b: nop

Adding the [STATHread] attribute to a C# app fixes the issue with leaving Excel open after the app quits and makes the last four lines unecessary.

  • Related