如何在桌面应用程序中实现内存 [英] how to realese memory in desktop application

查看:82
本文介绍了如何在桌面应用程序中实现内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我做了一个桌面应用程序.在我的项目中,我使用一些word和excel文件.
当我关闭应用程序时,某些"Winworld.exe"仍在任务管理器中运行.
当我关闭应用程序时,如果要删除该特定的Word文件,则无法删除.我想释放内存.

在此先感谢.

Hi,
I made a desktop application. In my project i use some word and excel file.
when i close the application some "Winworld.exe" is still running in task manager.
and when i close the application then if i want to delete that particular word file it can''t delete.I want to release the memory.

Thanks in Advance.

推荐答案



尝试检查以下链接:
http://stackoverflow.com/questions/7070124/c-sharp-msoffice-interop-word-will-not-kill-winword-exe [ http://support.microsoft.com/default.aspx?scid=kb;zh-我们; 317109 [ ^ ]
问候
Robert
Hi,

try to check following links:
http://stackoverflow.com/questions/7070124/c-sharp-msoffice-interop-word-will-not-kill-winword-exe[^]
http://support.microsoft.com/default.aspx?scid=kb;en-us;317109[^]
Regards
Robert


您应始终跟踪打开的单词并在应用程序退出时使用excel应用程序将其关闭.

这是发布word或excel应用程序的代码.这是针对word应用程序的,但请更改并将其用于excel应用程序:
You should always track opened word and excel applications to close them when your application exits.

This is the code to release the word or excel application. This is for word application but change and use it for excel applications :
wordApplication.Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);

System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApplication);



如果您的应用程序崩溃了,并且您没有机会关闭excel或word应用程序,则应使用以下特殊代码:

助手类:



If your application was crashed and you didn''t have the chance to close the excel or word application you should use this special code :

Helper class :

public static class ProcessExtension
{
    private static string FindIndexedProcessName(int pid)
    {
        try
        {
            var processName = Process.GetProcessById(pid).ProcessName;

            var processesByName = Process.GetProcessesByName(processName);

            string processIndexdName = null;

            for (var index = 0; index < processesByName.Length; index++)
            {
                processIndexdName = index == 0 ? processName : processName + "#" + index;

                var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);

                if ((int)processId.NextValue() == pid)
                {
                    return processIndexdName;
                }
            }

            return processIndexdName;
        }
        catch
        {
            return "";
        }
    }

    private static Process FindPidFromIndexedProcessName(string indexedProcessName)
    {
        var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);

        try
        {
            return Process.GetProcessById((int)parentId.NextValue());
        }
        catch
        {
            return null;
        }
    }

    public static Process Parent(this Process process)
    {
        return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
    }
}


这段代码的某些部分是从stackoverflow中提取的.

并通过这种方式使用它:


Some parts of this code were extracted from stackoverflow.

And use it by this way :

Process parentProcess;

foreach (var process in Process.GetProcesses().Where(i => i.ProcessName == "WINWORD"))
{
    parentProcess = process.Parent();

    if ((parentProcess != null) && (string.Compare(parentProcess.ProcessName, "svchost", true) == 0))
    {
        process.Kill();
    }
}


这将杀死其父级为svchost 的所有WORD进程.

如果用户启动excel或word应用程序,则其父级将为explorer.

对其进行更改,以使其可以杀死excel进程.

希望对您有所帮助.


This will kill all WORD processes which their parent is svchost .

If user starts an excel or word application its parent will be explorer.

Change it so that it can kill excel processes.

Hope it helps.


这篇关于如何在桌面应用程序中实现内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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