Kill Specified Task instead of Process in C#

1.3k views Asked by At

Like Microsoft Word, If we open multiple Word windows, then we will find that they are under one process named WINWORD with multiple tasks under it.

In task manager of Window 8.1 Pro, we can right click it and end it by End Task.

I success to do the end process with process name but I cannot get any idea from Google search that how to do the kill certain task under certain process in this situation.

Emphasize: I am not asking how to kill the process.

processes[i].Kill();

Update

I success to kill the specified Word Window with

using System;
using System.Runtime.InteropServices;

namespace CloseTask
{
    class Program
    {
        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName,string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_CLOSE = 0xF060;

        static void Main(string[] args)
        {
            closeWindow();
        }

        private static void closeWindow()
        {
            // retrieve the handler of the window of Word
            // Class name, Window Name
            int iHandle = FindWindow("OpusApp", "Document1 - Microsoft Word");
            if (iHandle > 0)
            {
                //close the window using API
                SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
            }
        }   
    }
}

But I wonder how can I kill the oldest Word Window in this case? If process can sort the StartTime to kill the oldest one.... But I think this should be included in another question, thanks.

1

There are 1 answers

6
zmbq On BEST ANSWER

Well, from the looks of it, the Task Manager displays multiple entries if your application has multiple top-level Windows, even if they all belong to the same process.

When you click on End Task, the Task Manager sends a WM_CLOSE message to the selected window. It is up to your application to close just one window and not the others.