因为A程序在退出时有相关处理,所以不能直接杀掉A的进程。目前我是想通过B发送一个WINDOWS消息给A,A收到后,自己关闭。请问如何实现?
解决方案 »
- vs2008自带打包覆盖安装问题
- System.Threading.Timer
- .NET数据操作时 大家习惯用ado.net 还是习惯用各种数据源控件?哪种方法效率更高
- static Hashtable ht = new Hashtable();为什么有如下错误?
- VB.NET 的一个函数在C#中对应是什么?
- WindowsControlLibrary和WebControlLibrary
- 要开发数据库驱动程序,让ADO.net可以统一访问。给一些建议吧
- 将listbox绑定后要更改item内容的问题
- 高手帮忙呀。。解决立即结帖!
- 用c#写的dll能不能用在别的语言中,比如C++builder?
- 在 .net 中 ComboBox 控件是不是没有只读这个属性
- 怎么判断DataGrid内是否有值!
还有,WM_USER是多大的值?
p.kill();
Process.CloseMainWindow 方法 通过向进程的主窗口发送关闭消息来关闭拥有用户界面的进程。 返回值
如果成功发送了关闭消息,则为 true;如果关联进程没有主窗口或禁用了主窗口(例如,如果当前显示模式对话框),则为 false。
using System.Diagnostics;
using System.Threading;namespace Process_Sample
{
class MyProcessClass
{
public static void Main()
{
try
{
Process myProcess;
myProcess = Process.Start("Notepad.exe");
// Display physical memory usage 5 times at intervals of 2 seconds.
for (int i = 0;i < 5; i++)
{
if (!myProcess.HasExited)
{
// Discard cached information about the process.
myProcess.Refresh();
// Print working set to console.
Console.WriteLine("Physical Memory Usage: "
+ myProcess.WorkingSet.ToString());
// Wait 2 seconds.
Thread.Sleep(2000);
}
else {
break;
}
} // Close process by sending a close message to its main window.
myProcess.CloseMainWindow();
// Free resources associated with process.
myProcess.Close(); }
catch(Exception e)
{
Console.WriteLine("The following exception was raised: ");
Console.WriteLine(e.Message);
}
}
}
}
1、发windows消息
2、通过进程
3、通过读写中间文件
4、用MSMQ
......立即成为编程经验丰富的程序员不是梦,详见:http://www.psec.net.cn
UM_CLOSEWINDOW = WM_USER+1000;Process[] p = Process.GetProcessesByName(APP_NAME);
if (p.Length > 0)
{
IntPtr mainWindowHandle = p[0].MainWindowHandle;
if (mainWindowHandle == IntPtr.Zero) //缩小至托盘时MainWindowHandle为零
{
mainWindowHandle = FindWindow(null, "标题名称");
bool ok = SendMessage(mainWindowHandle, UM_CLOSEWINDOW, IntPtr.Zero, IntPtr.Zero);
}
}[DllImport("user32.dll", SetLastError = true)]
public static extern bool SendMessage(IntPtr hWnd, int nMsg, IntPtr wParam, IntPtr lParam);[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg == UM_CLOSEWINDOW)
{
//TODO:保存操作
this.Close();
}
base.WndProc(ref m);
}