【发布时间】:2015-09-03 18:50:44
【问题描述】:
就像Microsoft Word一样,如果我们打开多个Word窗口,那么我们会发现它们在一个名为WINWORD的进程下,在它下有多个任务。
在Window 8.1 Pro的任务管理器中,我们可以右键单击它并通过结束任务结束它。
我成功地用进程名称完成了结束进程,但我无法从谷歌搜索中得到任何关于在这种情况下如何在特定进程下执行终止特定任务的想法。
强调:我不是在问如何杀死进程。
进程[i].Kill();
更新
我成功杀死了指定的Word窗口
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);
}
}
}
}
但我想知道在这种情况下如何杀死 oldest Word Window?如果进程可以对 StartTime 进行排序以杀死最旧的......但我认为这应该包含在另一个问题中,谢谢。
【问题讨论】:
-
@Damien_The_Unbeliever ,不,这不是视觉分组。因为我检查了 Process.GetProcesses() 结果,即使打开了多少 Microsoft Word 窗口,也始终只有一个 WINWORD 进程。
-
您可以通过使用 Interop 类或非托管代码(例如 codeproject.com/KB/shell/…)来完成此操作
标签: c# console-application terminate