我有两个exe运行的C#控制台程序。从一个,我需要告诉第二个exe做某事?我怎样才能做到这一点?我看了
(Remotable.CommonAssembly)Activator.GetObject(typeof(Remotable.CommonAssembly)
但是从这里我可以调用CommonAssembly(引用的dll)的方法而不是exe的方法。
答案 0 :(得分:5)
对于简单的场景,一个普通的旧窗口事件就足够了 - 一个程序等待发信号通知做某事。
在等待事件的等待程序中生成一个线程。
//Program 1
EventWaitHandle evt = OpenOrCreateEvent("Global\\MyEvent");
evt.WaitOne(); // this thread will block waiting without wasting CPU cycles,
// it will be be signaled from the kernel
// when the event is set
DoStuff();
//Program 2
EventWaitHandle evt = OpenOrCreateEvent("Global\\MyEvent");
evt.Set();
查看EventWaitHandle,ManualResetEvent,AutoResetEvent类。
如果你有更复杂的通信协议,不同的动作要触发,返回值等,那么像WCF,Remoting,DCOM,CORBA等高级IPC机制可能会更好。对于简单的情况(一对)窗口事件就足够了。
<强>通知强> 如果需要在进程间传输数据,请考虑内存映射文件。 .NET 4.0将提供“官方”.NET类,目前您可以使用http://github.com/tomasr/filemap/tree/master
答案 1 :(得分:3)
查看WCF以了解.NET中的进程间通信。有多种协议可用于同一台机器或远程机器通信。对于同一台机器,我建议检查命名管道或.NET TCP绑定。
WCF有一个轻微的学习曲线,但是有很多教程,这是绝对值得学习的东西。
答案 2 :(得分:2)
答案 3 :(得分:2)
这应该可以帮到你......
[代码]
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace TestApp
{
public static class Messenger
{
public const uint WM_USER = 0x0400;
public const int MyMessage = 0x00000001;;
[DllImport("User32.dll")]
private static extern int RegisterWindowMessage(string lpString);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
internal static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
//For use with WM_COPYDATA and COPYDATASTRUCT
[DllImport("User32.dll", EntryPoint = "SendMessage")]
internal static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
//For use with WM_COPYDATA and COPYDATASTRUCT
[DllImport("User32.dll", EntryPoint = "PostMessage")]
internal static extern int PostMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
internal static extern uint SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "PostMessage")]
internal static extern int PostMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
internal static extern bool SetForegroundWindow(int hWnd);
//Used for WM_COPYDATA for string messages
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
internal static int sendWindowsStringMessage(int hWnd, int wParam, string msg)
{
int result = 0;
if (hWnd > 0)
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(msg);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = msg;
cds.cbData = len + 1;
result = SendMessage(hWnd, (int)WM_USER, wParam, ref cds);
}
return result;
}
internal static uint sendWindowsMessage(IntPtr hWnd, uint Msg, IntPtr wParam, int lParam)
{
uint result = 0;
if (hWnd != IntPtr.Zero)
{
result = SendMessage(hWnd, Msg, wParam, lParam);
}
return result;
}
internal static IntPtr getWindowId(string className, string windowName)
{
return FindWindow(className, windowName);
}
}
}
在您的通话方式中:
uint result = 0;
IntPtr hWnd = Messenger.getWindowId(null, "MyOtherApp-Window_Title");
result = Messenger.sendWindowsMessage(hWnd, Messenger.WM_USER, Handle, Messenger.MyMessage);
[/代码]
答案 4 :(得分:1)
你可能会考虑IPC。 NamedPipeClientStream和NamedPipeServerStream非常简单。 Here's an example其中IPC用于将命令行参数从应用程序的一个实例传递到另一个实例。这不完全是你想做的,但非常接近。