9

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.

V-SHY
  • 3,925
  • 4
  • 31
  • 47
  • @Damien_The_Unbeliever , No, it is not visual grouping. Because I checked with the Process.GetProcesses() result and there always have one and only one WINWORD process even though how many Microsoft Word windows were opening. – V-SHY Jun 18 '15 at 08:41
  • You can do it by using Interop classes or unmanaged code such as http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx?fid=422864&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=2277170 – Deepak Mishra Jun 18 '15 at 09:56

1 Answers1

4

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.

zmbq
  • 38,013
  • 14
  • 101
  • 171
  • Any easy way to send the WM_CLOSE message in C#? I am thinking how to get the window handle to close the specified task. Can we obtain any window info from known window handle? thanks – V-SHY Jun 18 '15 at 08:56
  • http://stackoverflow.com/questions/1888863/how-to-get-main-window-handle-from-process-id – zmbq Jun 18 '15 at 09:01
  • Sending a message is done by calling the Win32 API PostMessage (or SendMessage if you want to see how the other side handled it, but then you need to be certain not to do that from your UI thread) – zmbq Jun 18 '15 at 09:02
  • I will give it a try now, I found that I can use Spy++ to get the Handle, Caption(Window Name), Class Name by drag and drop its Finder tool to different Visible window – V-SHY Jun 18 '15 at 09:04
  • I can kill it but I wonder how can I kill it without knowing the classname and the window name.... – V-SHY Jun 18 '15 at 10:26
  • Seems like an issue for a whole new question. – zmbq Jun 18 '15 at 10:28