使用VB.NET关闭服务器中的单词进程

问题描述:

我在这里得到了一些生成word文件的代码。它在我的本地主机和测试服务器中运行良好。 winword.exe正在关闭并释放内存。但是,在测试它到另一台服务器时,winword.exe仍然保留在后台进程中,即使在生成后内存也不会被释放。

我监视服务器的CPU使用率,它甚至不高生成word文件后返回0%,但winword.exe仍然在后台。

我是新手。请帮忙。谢谢!



我尝试过:



Public Sub Generate_Word( ByVal strResult作为List(字符串))

Dim strTemplateFile As String =〜\ TestFolder \TestFile.doc

将WordApp作为新Word.Application

Dim newDoc As Word.Document

newDoc = WordApp.Documents.Add(HttpContext.Current.Server.MapPath(strTemplateFile))



'***在这里生成word文件***'



'***将文件保存在这里***'



ReleaseObject(rng)

newDoc.Close(False)

ReleaseObject(newDoc)


WordApp.Application.Quit(错误)

ReleaseObject(WordApp)



'****** *****开始清理代码*************'

GC.Collect()

GC.WaitForPendingFinalizers()



'GC n为了让终结者第一次被称为

',需要两次调用,它只是列出了什么是最终确定的,是第二次在,它实际上是最终确定。只有

'然后对象会自动执行ReleaseComObject。



GC.Collect()

GC。 WaitForPendingFinalizers()

'***********清理代码结束*************'



'***将文件保存到数据库***'

结束子



Public Sub ReleaseObject(ByVal obj As Object)

尝试

Dim intRel As Integer = 0

Do

intRel = System .Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)

Loop while intRel> 0

obj =没什么

Catch ex As Exception

obj =没什么

最后



结束尝试

结束Sub

I got some codes here which is generating a word file. It is working fine in my local host and in the test server. The winword.exe is properly closing and releasing the memory. However, upon testing it to the another server, the winword.exe still remain in background process and memory is not released even after the generation.
I monitored the CPU usage of the server and it's not even high and goes back to 0% after word file generation but the winword.exe remains in background.
I am new to this. Please help. Thanks!

What I have tried:

Public Sub Generate_Word(ByVal strResult As List(Of String))
Dim strTemplateFile As String = "~\TestFolder\TestFile.doc"
Dim WordApp As New Word.Application
Dim newDoc As Word.Document
newDoc = WordApp.Documents.Add(HttpContext.Current.Server.MapPath(strTemplateFile))

'*** Generate word file here ***'

'*** Save the file here***'

ReleaseObject(rng)
newDoc.Close(False)
ReleaseObject(newDoc)

WordApp.Application.Quit(False)
ReleaseObject(WordApp)

'*********** Start of clean up code *************'
GC.Collect()
GC.WaitForPendingFinalizers()

'GC needs to be called twice in order to get the Finalizers called
'the first time in, it simply makes a list of what is to be
'finalized, the second time in, it actually is finalizing. Only
'then will the object do its automatic ReleaseComObject.

GC.Collect()
GC.WaitForPendingFinalizers()
'*********** End of clean up code *************'

'*** save the file to database here ***'
End Sub

Public Sub ReleaseObject(ByVal obj As Object)
Try
Dim intRel As Integer = 0
Do
intRel = System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
Loop While intRel > 0
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally

End Try
End Sub

不支持自动化Word(或任何Office应用程序)在asp.net下,微软从未说过这会起作用,他们反过来说了。使用其他方法制作asp.net支持的文档,如DocX,Office XML SDK,Aspose等。
Automating Word (or any Office app) is not supported under asp.net, Microsoft have never said this is going to work, they have said the opposite. Use something else to make your documents that is supported under asp.net such as DocX, Office XML SDK, Aspose and so on.