我正在尝试将参数发送到已经在处理器中的应用程序。我正在使用Mutex来查找应用程序是否正在运行。我需要发送任何命令行参数,并将该文本添加到列表框中。但是参数进入但是值没有添加到列表框中。应用程序的名称是“MYAPPLICATION”,将值添加到列表框的函数是parameters()
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 Frm1 = new Form1();
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "MYAPPLICATION", out createdNew)) //Finding if application is running or not
{
if (createdNew)
{
//foreach (string abc in Environment.GetCommandLineArgs())
//{
// MessageBox.Show(abc);
//}
Application.Run(Frm1);
}
else
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
Frm1.parameters(Environment.GetCommandLineArgs());
break;
}
}
}
}
}
答案 0 :(得分:3)
在Windows 7中仍然有效我们在最近的应用程序中实现了同样的功能,它可以在所有Windows平台上完美运行。 (测试回到Windows XP,但在Windows 98中使用相同的api)
这是codeproject的链接。
http://www.codeproject.com/KB/cs/ipc_wmcopy.aspx
基本上在应用程序中注册一个窗口并向该窗口发送消息。然后,您可以将其过滤到应用程序。
非常酷,效率很高。使用一个小工具,您可以使无限量的应用程序实例相互通信。
答案 1 :(得分:0)
消息队列是进程间通信的常见模式。 Volure的版本很酷。更常见的方法是使用MSMQ(Microsoft消息队列)。
在这里查看
http://msdn.microsoft.com/en-us/library/ms978430.aspx
http://en.wikipedia.org/wiki/Microsoft_Message_Queuing
http://blog.goyello.com/2009/09/08/why-msmq-is-excelent-for-net-developers/