在大多数情况下,我必须自己弄清楚。
方法如下。
首先:我们有流程。该进程没有主窗口,根本没有窗口,因此与窗口的所有通信都是不可行的。但是进程有线程。并且线程有它们的消息队列,虽然它可以通过 Win32 API 访问。
所以这里是接收端:
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application {
// (...)
/// <summary>
/// Sent as a signal that a window or an application should terminate.
/// </summary>
const uint WM_CLOSE = 0x0010;
/// <summary>
/// Retrieves a message from the calling thread's message queue. The function dispatches incoming sent messages until a posted message is available for retrieval.
/// </summary>
/// <param name="lpMsg">MSG structure that receives message information from the thread's message queue.</param>
/// <param name="hWnd">A handle to the window whose messages are to be retrieved. The window must belong to the current thread. Use <see cref="IntPtr.Zero"/> to retrieve thread message.</param>
/// <param name="wMsgFilterMin">The integer value of the lowest message value to be retrieved.</param>
/// <param name="wMsgFilterMax">The integer value of the highest message value to be retrieved.</param>
/// <returns>Non-zero for any message but WM_QUIT, zero for WM_QUIT, -1 for error.</returns>
[DllImport("user32.dll")]
static extern int GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
/// <summary>
/// Waits indefinitely for a specific thread message.
/// </summary>
/// <param name="signal">Signal, message value.</param>
private void WaitForSignal(uint signal) => GetMessage(out var msg, IntPtr.Zero, signal, signal);
/// <summary>
/// WPF application startup.
/// </summary>
/// <param name="e">Event arguments.</param>
protected override async void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
// ... initialization code ...
await Task.Run(() => WaitForSignal(WM_CLOSE));
Shutdown();
}
// (...)
}
发送端:
/// <summary>
/// Demo.
/// </summary>
class Program {
/// <summary>
/// Sent as a signal that a window or an application should terminate.
/// </summary>
const uint WM_CLOSE = 0x0010;
/// <summary>
/// Posts a message to the message queue of the specified thread. It returns without waiting for the thread to process the message.
/// </summary>
/// <param name="threadId">The identifier of the thread to which the message is to be posted.</param>
/// <param name="msg">The type of message to be posted.</param>
/// <param name="wParam">Additional message-specific information.</param>
/// <param name="lParam">Additional message-specific information.</param>
/// <returns></returns>
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostThreadMessage(uint threadId, uint msg, IntPtr wParam, IntPtr lParam);
/// <summary>
/// Closes a windowless application pointed with process name.
/// </summary>
/// <param name="processName">The name of the process to close.</param>
static void CloseWindowless(string processName) {
foreach (var process in Process.GetProcessesByName(processName)) {
using (process) {
foreach (ProcessThread thread in process.Threads) PostThreadMessage((uint)thread.Id, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
}
/// <summary>
/// Main application entry point.
/// </summary>
/// <param name="args">Command line arguments.</param>
static void Main(string[] args) => CloseWindowless("MyAppName");
}
背后的巫术魔法:P/Invoke 部分非常明显。更有趣的是 WPF Application.OnStartup 覆盖行为。
如果方法是同步的并且不退出,则应用程序挂起。
但是,如果它被标记为异步,则不必退出。它无限期地等待任何可以等待的东西。这正是我们所需要的,因为我们只能从主 UI 线程调用 Shutdown()。我们不能阻塞那个线程,所以我们必须等待另一个线程上的消息。我们将 WM_CLOSE 发送到所有进程线程,所以它会得到它。当它结束时,会从主线程调用Shutdown 方法,就是这样。
此解决方案的最佳之处在于它不需要 System.Windows.Forms 参考。