当前WinForm程序强制关闭另一个WinForm程序,
因为另一个程序在运行的时候会在任务栏上显示一个图标,
所以在强制杀除时,任务栏上的图标没有消失,要通过鼠标移到任务栏上的图片上,才会消失。
求如何解决?代码如下: /// <summary>
/// 杀死应用程序
/// </summary>
/// <param name="appname"></param>
protected void KillMainApp(string mainAppExe)
{
Process[] allProcess = Process.GetProcesses();
foreach (Process p in allProcess)
{
if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower())
{
if (p.CloseMainWindow() == false)
{
p.Kill();
Thread.Sleep(1500);
LogHelper.WriteLog("kill"+ p.ProcessName);
}
else
{
p.WaitForExit(5000);
LogHelper.WriteLog("close" + p.ProcessName);
}
}
}
}
c#winform
因为另一个程序在运行的时候会在任务栏上显示一个图标,
所以在强制杀除时,任务栏上的图标没有消失,要通过鼠标移到任务栏上的图片上,才会消失。
求如何解决?代码如下: /// <summary>
/// 杀死应用程序
/// </summary>
/// <param name="appname"></param>
protected void KillMainApp(string mainAppExe)
{
Process[] allProcess = Process.GetProcesses();
foreach (Process p in allProcess)
{
if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower())
{
if (p.CloseMainWindow() == false)
{
p.Kill();
Thread.Sleep(1500);
LogHelper.WriteLog("kill"+ p.ProcessName);
}
else
{
p.WaitForExit(5000);
LogHelper.WriteLog("close" + p.ProcessName);
}
}
}
}
c#winform
我是 Kill 掉了。
[DllImport("shell32.dll")]
static extern bool Shell_NotifyIcon(uint dwMessage, [In] ref NOTIFYICONDATA pnid);
protected void KillMainApp(string mainAppExe)
{
Process[] allProcess = Process.GetProcesses();
foreach (Process p in allProcess)
{
if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower())
{
IntPtr hdl = p.MainWindowHandle;
int result = SendMessage(hdl, WM_CLOSE, 0, 0);
}
}
}
经过测试,无法杀除程序。是否这种写法有问题?
通过鼠标模拟点击任务栏,使其刷新任务栏。
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect);
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private const int SM_CXSMICON = 49;
private const int SM_CYSMICON = 50;
[DllImport("user32")]
public static extern int GetSystemMetrics( int nIndex ); [DllImport("user32.dll")]
public static extern bool GetCursorPos(ref Point lpPoint);
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
private static extern int SetCursorPos(int x, int y);
[DllImport("user32.dll", EntryPoint = "RedrawWindow")]
public static extern int RedrawWindow(IntPtr hwnd, ref Rect lprcUpdate, int hrgnUpdate, int fuRedraw);int TrayWindow;
Rect WindowRect;
int SmallIconWidth;
int SmallIconHeight;
Point CursorPos = Point.Empty ;
int Row;
int Col;
int hwd = FindWindow("Shell_TrayWnd",null );
TrayWindow = FindWindowEx(hwd, 0, "TrayNotifyWnd", null);
int isGet = GetWindowRect(new IntPtr(TrayWindow), out WindowRect);
if (isGet == 0) return; SmallIconWidth = GetSystemMetrics(SM_CXSMICON);
SmallIconHeight = GetSystemMetrics(SM_CYSMICON); GetCursorPos(ref CursorPos); for (Row = 0; Row <= (WindowRect.Bottom - WindowRect.Top) / SmallIconHeight; Row++)
{
for (Col = 0; Col <= (WindowRect.Right - WindowRect.Left) / SmallIconWidth; Col++)
{
SetCursorPos(WindowRect.Left + Col * SmallIconWidth, WindowRect.Top + Row * SmallIconHeight);
Thread.Sleep(1);
}
} SetCursorPos(CursorPos.X, CursorPos.Y); Rect tempRect = new Rect();
RedrawWindow( new IntPtr( TrayWindow) , ref tempRect, 0, 0x85);