gpt4 book ai didi

c# - Microsoft.Interop 对象不会退出或 "release"

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:25 26 4
gpt4 key购买 nike

每次我运行这段代码时,对象都不会关闭。我仍然有一个 excel.exe 在任务管理器中运行。即使我设置了 objects = null,仍然没有。我什至尝试过使用对象的 .Quit() 方法。

我在这里做错了什么?

    private bool ValidateQM()
{
//setup the objects
Excel.Application oXL = null;
Excel.Workbook oWB = null;
Excel.Worksheet oSheet = null;
int hWnd = 0;

try
{

//Start Excel and get Application object.
oXL = new Excel.Application();
hWnd = oXL.Application.Hwnd;
oXL.Visible = false;

//Open the workbook.
oWB = oXL.Workbooks.Open(workingForm, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",true, false, 0, true, false, false);

//Get the Worksheet
oSheet = oWB.Worksheets[1];

//Check the date values

string mydatetime = oSheet.Cells[5, 33].Text.ToString() + " " + oSheet.Cells[7, 33].Text.ToString();
string dateofscore = oSheet.Cells[3, 12].Text.ToString();

DateTime.Parse(mydatetime); //make my string a real boy
DateTime.Parse(dateofscore);

// Cleanup
GC.Collect();
GC.WaitForPendingFinalizers();

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oSheet);

//oWB.Close();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oWB);

//oXL.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oXL);

return true;
}

最佳答案

您是否违反了 hWnd = oXL.Application.Hwnd 行中的“不要使用 2 个点”规则?参见 Excel interop libraries incompatible with ASP.NET?有关更多信息。

关于c# - Microsoft.Interop 对象不会退出或 "release",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9541187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com