过程在后台进程上打开多个Application实例 [英] Procedure opens multiple instances of Application on background processes

查看:66
本文介绍了过程在后台进程上打开多个Application实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



我使用以下程序从Windows窗体打开工作表。它运行正常,我遇到的问题是,每当我单击表单上的按钮转到特定工作表时,它会在后台进程中创建一个新的Excel实例。在应用程序中只有一个实例,但是后台进程,我看到每当我点击转到表单上的任何工作表时创建的多个Excel。



我尝试使用以下内容来发布后台进程:

 System.Runtime.InteropServices.Marshal.ReleaseComObject (xlSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)





它有效,但因为我有一个关闭winform和Excel的按钮,每当我把它放在我的代码末尾时,表单就会关闭,但不会关闭Excel。



如何防止这种情况发生?这不是我的计算机上的问题,但我的客户端的PC将运行多个资源用户繁重的应用程序,因此在后台进程下打开多个实例会使PC更慢。如何防止这种情况发生?



这是我打开并检查Excel的代码:



 Public Sub GoToSheets(sheetName As String)

'此子用于在所选工作表上打开工作簿。
'这将检查Excel工作簿是否已打开,如果没有,
'将打开Excel,工作簿,然后打开所选工作表。如果工作簿打开
',则会转到所选工作表。

'@ param sheetName,要显示的工作表

Dim xlApp As Excel.Application

尝试
'获取现有的Excel。应用程序对象
xlApp = CType(GetObject(,Excel.Application),Application)
Catch ex As Exception
'没有现有的excel.application对象 - 创建一个新的

xlApp =新Excel.Application
结束尝试

Dim xlWBName As String =2011.1004.Compensation Template
Dim xlBookPath As String = Path.Combine(Directory。 GetCurrentDirectory())

xlApp.Visible = True

尝试
'获取打开的工作簿
xlBook = xlApp.Workbooks(xlWBName&。 xlsx)
Catch ex As Exception
'open it
xlBook = xlApp.Workbooks.Open(xlBookPath&\& xlWBName&.xlsx)
结束尝试

xlSheet = CType(CType(xlBook.Sheets(sheetName),Excel.Worksheet),Excel.Worksh e))

frmNavigation.Close()
xlSheet.Activate()
DashboardView()

frmWelcomePage.Hide()
chkForm( )

End Sub





这是我在表单上的关闭按钮上使用的代码:



 Sub closeXLApp()

'调用此子程序以关闭应用程序而不使用
'保存任何更改到工作簿。子关闭
'应用程序,工作簿和工作表并执行一些垃圾清理
'以及确保打开的Excel实例从内存中清除。

xlBook.Close(SaveChanges:= False)
xlApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing

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

End Sub

解决方案

在表单的unload事件中写入关闭(或终止)所有excel进程的代码

Hello

I use the below procedure to open a worksheet from a windows form. It works fine, the problem I have is that whenever I click on a button on my form to go to a particular sheet it creates a new instance of Excel in the background process. In the Apps there is only one instance, but the background processes, I see multiple Excel which are created whenever I click to go to any sheet on my form.

I tried using the following to release the background processes:

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)



It worked but because I have a button that closes the winform and Excel, whenever I put that at the end of my code the form would close, but not Excel.

How can I prevent that from happening? It is not an issue on my computer, but my client's PC will be running multiple applications which are resource user heavy so having multiple instances open under background process will slow down their PC even more. How can I prevent this from happening?

Here is my code to open and check for Excel:

 Public Sub GoToSheets(sheetName As String)

    'This sub is used to open the workbook on the selected sheet.
    'This checks to see if Excel workbook is opened, if not it
    'opens Excel, the workbook and then the selected sheet. If the workbook is
    'opened, it goes to the selected sheet.

    '@param sheetName, sheet to be displayed

    Dim xlApp As Excel.Application

    Try
        'get an existing excel.application object
        xlApp = CType(GetObject(, "Excel.Application"), Application)
    Catch ex As Exception
        'no existing excel.application object - create a new one

        xlApp = New Excel.Application
    End Try

    Dim xlWBName As String = "2011.1004.Compensation Template"
    Dim xlBookPath As String = Path.Combine(Directory.GetCurrentDirectory())

    xlApp.Visible = True

    Try
        'get the opened workbook
        xlBook = xlApp.Workbooks(xlWBName & ".xlsx")
    Catch ex As Exception
        'open it
        xlBook = xlApp.Workbooks.Open(xlBookPath & "\" & xlWBName & ".xlsx")
    End Try

    xlSheet = CType(CType(xlBook.Sheets(sheetName), Excel.Worksheet), Excel.Worksheet)

    frmNavigation.Close()
    xlSheet.Activate()
    DashboardView()

    frmWelcomePage.Hide()
    chkForm()

End Sub



Here is the code I use on the Close button on my form:

Sub closeXLApp()

'This sub is called to close the application without
'saving any changes to the workbook. The sub closes
'the app, workbook and sheet and performs some garbage clean up
'as well making sure that the opened Excel instance is cleared from memory.

xlBook.Close(SaveChanges:=False)
xlApp.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing

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

End Sub

解决方案

In the unload event of the form write the code for closing (or killing) all the excel processes


这篇关于过程在后台进程上打开多个Application实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆