C# 如何在WinForms应用程序的另一个流程实例中调用方法?
我正在开发一个使用互斥体的应用程序,以确保它是系统上运行的应用程序的唯一实例 当应用程序的另一个实例尝试启动时,我希望在原始实例中运行一个方法 我可以从应用程序的另一个实例调用应用程序中的特定方法吗 我发现了一些通过将消息发送到HWND_BROADCAST来使用RegisterWindowMessage/PostMessage Win32 API的示例,但我无法让它们正常工作,而且我在其他地方读到过使用HWND_BROADCAST可能会有危险C# 如何在WinForms应用程序的另一个流程实例中调用方法?,c#,winforms,inter-process-communicat,C#,Winforms,Inter Process Communicat,我正在开发一个使用互斥体的应用程序,以确保它是系统上运行的应用程序的唯一实例 当应用程序的另一个实例尝试启动时,我希望在原始实例中运行一个方法 我可以从应用程序的另一个实例调用应用程序中的特定方法吗 我发现了一些通过将消息发送到HWND_BROADCAST来使用RegisterWindowMessage/PostMessage Win32 API的示例,但我无法让它们正常工作,而且我在其他地方读到过使用HWND_BROADCAST可能会有危险 有没有更好的方法不需要让应用程序在特权模式下运行?我
有没有更好的方法不需要让应用程序在特权模式下运行?我以前做过这方面的研究-你可以使用本文中演示的内存映射文件,或者你可以像我那样做(简单的方法)并利用vb.net的功能(特别是,它允许您制作单实例应用程序,并在当前运行的实例中调用通过命令行args传递的方法[以便您可以使用它来调用应用程序中的方法]).我知道在C#中使用VB类听起来有点糟糕,但这是最抽象、最简单的方法。链接到相关文章-的最后一部分这里是我写的一个小助手 要使用它:
var pipeListener = new NamedPipeListener<String>(); // instantiate an instance
pipeListener.MessageReceived += (sender, e) => MessageBox.Show(e.Message); // when a message is received, show a messagebox with the message
pipeListener.Error += (sender, e) => MessageBox.Show("Error ({0}): {1}", e.ErrorType, e.Exception.Message); // oh noes!
pipeListener.Start(); // when you're ready, start listening
var pipeListener=new NamedPipeListener();//实例化一个实例
pipeListener.MessageReceived+=(发件人,e)=>MessageBox.Show(e.Message);//收到消息时,显示包含消息的MessageBox
pipeListener.Error+=(sender,e)=>MessageBox.Show(“Error({0}):{1}”,e.ErrorType,e.Exception.Message);//哦,不!
pipeListener.Start();//准备好后,开始侦听
从另一个过程:
NamedPipeListener<String>.SendMessage("Howdy howdy howdy");
NamedPipeListener.SendMessage(“Howdy-Howdy-Howdy”);
请注意,它使用PipeListener的全名作为管道的默认名称。如果需要更加谨慎,请使用采用管道名称的构造函数重载
下面是课程:
using System;
using System.IO.Pipes;
using System.Runtime.Serialization.Formatters.Binary;
namespace FunWithNamedPipes
{
/// <summary>Contains event data for <see cref="NamedPipeMessageReceiveHandler{TMessage}" /> events.</summary>
/// <typeparam name="TMessage"></typeparam>
public class NamedPipeListenerMessageReceivedEventArgs<TMessage> : EventArgs
{
/// <summary>Initializes an instance of <see cref="NamedPipeListenerMessageReceivedEventArgs{TMessage}" /> with the specified <paramref name="message" />.</summary>
/// <param name="message">The message passed by the event.</param>
public NamedPipeListenerMessageReceivedEventArgs(TMessage message)
{
this.Message = message;
}
/// <summary>Gets the message passed by the event.</summary>
public TMessage Message { get; private set; }
}
/// <summary>Contains event data for <see cref="NamedPipeListenerErrorEventHandler" /> events.</summary>
public class NamedPipeListenerErrorEventArgs : EventArgs
{
/// <summary>Initializes an instance of <see cref="NamedPipeListenerErrorEventArgs" /> with the specified <paramref name="errorType" /> and <paramref name="exception" />.</summary>
/// <param name="errorType">A <see cref="NamedPipeListenerErrorType" /> describing the part of the listener process where the error was caught.</param>
/// <param name="ex">The <see cref="Exception" /> that was thrown.</param>
public NamedPipeListenerErrorEventArgs(NamedPipeListenerErrorType errorType, Exception ex)
{
this.ErrorType = errorType;
this.Exception = ex;
}
/// <summary>Gets a <see cref="NamedPipeListenerErrorType" /> describing the part of the listener process where the error was caught.</summary>
public NamedPipeListenerErrorType ErrorType { get; private set; }
/// <summary>Gets the <see cref="Exception" /> that was caught.</summary>
public Exception Exception { get; private set; }
}
/// <summary>Represents a method that will handle an event where a message is received via named pipes.</summary>
/// <typeparam name="TMessage">The type of message that will be received.</typeparam>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The event data passed by the event, which includes the message that was received.</param>
public delegate void NamedPipeMessageReceivedHandler<TMessage>(Object sender, NamedPipeListenerMessageReceivedEventArgs<TMessage> e);
/// <summary>Represents a method that will handle an event that is fired when an exception is caught.</summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The event data passed by the event, included the error type and exception that was caught.</param>
public delegate void NamedPipeMessageErrorHandler(Object sender, NamedPipeListenerErrorEventArgs e);
/// <summary>Includes different types of errors that describe where in the listening process an exception was caught.</summary>
public enum NamedPipeListenerErrorType : byte
{
/// <summary>Indicates that an exception was caught while calling <see cref="NamedPipeServerStream.BeginWaitForConnection" />.</summary>
BeginWaitForConnection = 1,
/// <summary>Indicates that an exception was caught while calling <see cref="NamedPipeServerStream.EndWaitForConnection" />.</summary>
EndWaitForConnection = 2,
/// <summary>Indicates that an exception was caught while deserializing a message received from the named pipe.</summary>
DeserializeMessage = 3,
/// <summary>Indicates that an exception was caught while closing or disposing a used named pipe.</summary>
CloseAndDisposePipe = 4,
/// <summary>Indicates that an exception was caught while invoking the <see cref="NamedPipeListener{TMessage}.MessageReceived"/> event.</summary>
NotifyMessageReceived = 5
}
/// <summary>A helper class for sending and receiving messages using named pipes.</summary>
/// <typeparam name="TMessage">The type of message that will be sent or received.</typeparam>
public class NamedPipeListener<TMessage> : IDisposable
{
/// <summary>Occurs when a message is received.</summary>
public event NamedPipeMessageReceivedHandler<TMessage> MessageReceived;
/// <summary>Occurs when an exception is caught.</summary>
public event NamedPipeMessageErrorHandler Error;
static readonly String DEFAULT_PIPENAME = typeof(NamedPipeListener<TMessage>).FullName;
static readonly BinaryFormatter formatter = new BinaryFormatter();
NamedPipeServerStream pipeServer;
/// <summary>Initializes a new instance of <see cref="NamedPipeListener{TMessage}" /> using the specified <paramref name="pipeName" />.</summary>
/// <param name="pipeName">The name of the named pipe that will be used to listen on.</param>
public NamedPipeListener(String pipeName)
{
this.PipeName = pipeName;
}
/// <summary>Initializes a new instance of <see cref="NamedPipeListener{TMessage}" /> using the default pipe name.</summary>
/// <remarks>The default pipe name is the full name of the type of the instance.</remarks>
public NamedPipeListener()
: this(DEFAULT_PIPENAME) { }
/// <summary>The name of the named pipe that will be used to listen on.</summary>
public String PipeName { get; private set; }
/// <summary>Starts listening on the named pipe specified for the instance.</summary>
internal void Start()
{
if (pipeServer == null) pipeServer = new NamedPipeServerStream(DEFAULT_PIPENAME, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
try { pipeServer.BeginWaitForConnection(new AsyncCallback(PipeConnectionCallback), null); }
catch (Exception ex) { this.OnError(NamedPipeListenerErrorType.BeginWaitForConnection, ex); }
}
private void PipeConnectionCallback(IAsyncResult result)
{
try
{
pipeServer.EndWaitForConnection(result);
}
catch (Exception ex)
{
this.OnError(NamedPipeListenerErrorType.EndWaitForConnection, ex);
return;
}
TMessage message;
try
{
message = (TMessage)formatter.Deserialize(pipeServer);
}
catch (Exception ex)
{
this.OnError(NamedPipeListenerErrorType.DeserializeMessage, ex);
return;
}
try
{
this.OnMessageReceived(new NamedPipeListenerMessageReceivedEventArgs<TMessage>(message));
}
catch (Exception ex)
{
this.OnError(NamedPipeListenerErrorType.NotifyMessageReceived, ex);
return;
}
if (this.End())
{
this.Start();
}
}
internal Boolean End()
{
try
{
pipeServer.Close();
pipeServer.Dispose();
pipeServer = null;
return true;
}
catch (Exception ex)
{
this.OnError(NamedPipeListenerErrorType.CloseAndDisposePipe, ex);
return false;
}
}
private void OnMessageReceived(TMessage message)
{
this.OnMessageReceived(new NamedPipeListenerMessageReceivedEventArgs<TMessage>(message));
}
protected virtual void OnMessageReceived(NamedPipeListenerMessageReceivedEventArgs<TMessage> e)
{
if (this.MessageReceived != null)
{
this.MessageReceived(this, e);
}
}
private void OnError(NamedPipeListenerErrorType errorType, Exception ex)
{
this.OnError(new NamedPipeListenerErrorEventArgs(errorType, ex));
}
protected virtual void OnError(NamedPipeListenerErrorEventArgs e)
{
if (this.Error != null)
{
this.Error(this, e);
}
}
void IDisposable.Dispose()
{
if(pipeServer != null)
{
try { pipeServer.Disconnect(); }
catch { }
try { pipeServer.Close(); }
catch { }
try { pipeServer.Dispose(); }
catch { }
}
}
/// <summary>Sends the specified <paramref name="message" /> to the default named pipe for the message.</summary>
/// <param name="message">The message to send.</param>
public static void SendMessage(TMessage message)
{
NamedPipeListener<TMessage>.SendMessage(DEFAULT_PIPENAME, message);
}
/// <summary>Sends the specified <paramref name="message" /> to the specified named pipe.</summary>
/// <param name="pipeName">The name of the named pipe the message will be sent to.</param>
/// <param name="message">The message to send.</param>
public static void SendMessage(String pipeName, TMessage message)
{
using (var pipeClient = new NamedPipeClientStream(".", DEFAULT_PIPENAME, PipeDirection.Out, PipeOptions.None))
{
pipeClient.Connect();
formatter.Serialize(pipeClient, message);
pipeClient.Flush();
pipeClient.WaitForPipeDrain();
pipeClient.Close();
}
}
}
}
使用系统;
使用System.IO.Pipes;
使用System.Runtime.Serialization.Formatters.Binary;
命名空间FunWithNamedPipes
{
///包含事件的事件数据。
///
名为PipeListenerMessageReceivedEventArgs的公共类:EventArgs
{
///使用指定的初始化的实例。
///事件传递的消息。
公共名称PipeListenerMessageReceivedEventArgs(TMessage message)
{
this.Message=消息;
}
///获取事件传递的消息。
公共TMessage消息{get;private set;}
}
///包含事件的事件数据。
公共类名称PipeListenerErrorEventArgs:EventArgs
{
///使用指定的和初始化的实例。
///描述侦听器进程中捕获错误的部分的。
///被扔出去的那个。
公共名称PipeListenerErrorEventArgs(名称PipeListenerErrorType errorType,异常示例)
{
this.ErrorType=ErrorType;
这个.Exception=ex;
}
///获取描述侦听器进程中捕获错误的部分的。
public NamedPipeListenerErrorType错误类型{get;private set;}
///获取被捕获的数据。
公共异常异常{get;private set;}
}
///表示将处理通过命名管道接收消息的事件的方法。
///将接收的消息的类型。
///事件的来源。
///事件传递的事件数据,包括接收到的消息。
公共委托void NamedPipeMessageReceivedHandler(对象发送者,NamedPipeListenerMessageReceivedEventArgs e);
///表示一个方法,该方法将处理捕获异常时激发的事件。
///事件的来源。
///事件传递的事件数据包括捕获的错误类型和异常。
公共委托void NamedPipeMessageErrorHandler(对象发送者,NamedPipeListenerErrorEventArgs e);
///包括不同类型的错误,这些错误描述在侦听过程中捕获异常的位置。
公共枚举NamedPipeListenerErrorType:字节
{
///指示调用时捕获到异常。
BeginWaitForConnection=1,
///指示调用时捕获到异常。
EndWaitForConnection=2,
///指示在反序列化从命名管道接收的消息时捕获异常。
反序列化消息=3,
///指示在关闭或处理已使用的命名管道时捕获异常。
CloseAndDisposePipe=4,
///指示调用事件时捕获异常。
NotifyMessageReceived=5
}
///用于使用命名管道发送和接收消息的帮助器类。
///将要发送或接收的消息的类型。
公共类NamedPipeListener:IDisposable
{
///在收到消息时发生。
名为PipeMessageReceivedHandler MessageReceived的公共事件;
///捕获异常时发生。
公共事件NamedPipeMessageErrorHandler错误;
静态只读字符串默认值_PIPENAME=typeof(NamedPipeListener).FullName;
静态只读BinaryFormatter formatter=新的BinaryFormatter();
NamedPipeServerStream pipeServer;
///使用指定的参数初始化的新实例。
///将用于侦听的命名管道的名称。
公共名称PipeListener(字符串pipeName)
{
this.PipeName=管道名称;
}
///使用默认管道名称初始化的新实例。
///默认管道名称是实例类型的全名。
公共名称管道侦听器()
:此(默认管道名称){}