Click here to Skip to main content
14,298,783 members
Rate this:
Please Sign up or sign in to vote.
See more:
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.
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 2

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);


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));
    }
}

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();
    }
}

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.
   
Comments
BillWoodruff 28-Jan-12 10:51am
   
Looks like a good solid answer: the one question I have is: if the end-user had both an instance of Word, or Excel, launched, and was using Word, or Excel, within a .Net application: would your solution close all of the instances of Word ?
Amir Mahfoozi 28-Jan-12 13:28pm
   
Dear Bill, I have tested this code with two excel processes: one was started by a .NET application and the other was started by clicking on an .xls file. this code successfully killed the .NET started excel and left the user started one intact. However there are other conditions that can make this code seem silly! This code fulfilled our needs to some extents but if someone can define a not considered situation, of course he can develop its solution too. Thank you for your consideration again.
BillWoodruff 28-Jan-12 17:42pm
   
A very satisfying answer to a perhaps picky-picky question, but one asked out of true curiosity :) I have voted your answer the +5 it deserves !
Amir Mahfoozi 29-Jan-12 0:03am
   
Thank you Bill :)
Sergey Alexandrovich Kryukov 28-Jan-12 17:08pm
   
Very good, my 5.
--SA
Amir Mahfoozi 28-Jan-12 23:51pm
   
Thank you SA. :)
mayankshrivastava 30-Jan-12 4:50am
   
Thanks for reply Amir,
how to use your special code?
Amir Mahfoozi 30-Jan-12 5:23am
   
You will not work with ProcessExtension directly so place it in separate file then convert third code snippet to a function and manipulate it so that it can be used for both WINWORD and EXCEL. Good Luck.
mayankshrivastava 30-Jan-12 5:58am
   
Thanks a lot for code.....
5 stars........
Amir Mahfoozi 30-Jan-12 6:06am
   
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100