V-S*_*SHY 9 c# console-application terminate
与Microsoft Word一样,如果我们打开多个Word窗口,那么我们会发现它们位于一个名为WINWORD的进程下,其下有多个任务.
在Window 8.1 Pro的任务管理器中,我们可以右键单击它并通过End Task结束它.
我成功地使用流程名称来完成最终流程,但我无法从谷歌搜索中了解到在这种情况下如何在某些流程下杀死某些任务.
强调:我不是在问如何杀死这个过程.
过程[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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我想知道在这种情况下如何杀死最旧的 Word窗口?如果进程可以对StartTime进行排序以杀死最老的....但我认为这应该包含在另一个问题中,谢谢.
好吧,从表面上看,如果您的应用程序有多个顶级窗口,任务管理器会显示多个条目,即使它们都属于同一进程。
当您单击“结束任务”时,任务管理器会向选定的窗口发送一条WM_CLOSE消息。您的应用程序可以只关闭一个窗口而不关闭其他窗口。