2011-10-26 14 views
7

मैं एक ऐसे अनुप्रयोग पर काम कर रहा हूं जो म्यूटेक्स का उपयोग करता है यह सुनिश्चित करने के लिए कि यह सिस्टम पर चल रहे एप्लिकेशन का एकमात्र उदाहरण है।मैं अपने WinForms ऐप के किसी अन्य प्रक्रिया उदाहरण के भीतर एक विधि कैसे शुरू कर सकता हूं?

जब एप्लिकेशन का एक और उदाहरण शुरू करने का प्रयास करता है, तो मुझे मूल उदाहरण में चलाने की विधि चाहिए।

क्या मैं आवेदन के किसी अन्य उदाहरण से अपने आवेदन में एक विशिष्ट विधि का आह्वान कर सकता हूं?

मुझे HWND_BROADCAST को संदेश भेजकर RegisterWindowMessage/PostMessage Win32 API का उपयोग करके कुछ उदाहरण मिल गए हैं, लेकिन मैं उन्हें काम नहीं कर सका, और मैंने कहीं और पढ़ा है कि HWND_BROADCAST का उपयोग खतरनाक हो सकता है।

क्या ऐसा करने का कोई बेहतर तरीका है जिसमें ऐप को विशेषाधिकार प्राप्त मोड में चलाने की आवश्यकता नहीं है?

+2

आप एक hacky दृष्टिकोण, (और मेरा मतलब है hacky) चाहते हैं, जब एप्लिकेशन का एक दूसरा उदाहरण के प्रारंभ होने, कुछ अस्थायी स्थान में कुछ डमी फ़ाइल बनाते हैं, और ऐप ने नई बनाई गई फ़ाइल के लिए उस अस्थायी स्थान की निगरानी करने के लिए एक FileSystemWatcher का उपयोग किया है। जब यह पता चलता है कि एक नई फाइल बनाई गई है, तो आपको जो करना है वह करें :) – BFree

उत्तर

3

मैंने पहले इस पर शोध किया है - आप इस आलेख http://www.codeproject.com/KB/cs/singleinstanceapplication.aspx में प्रदर्शित स्मृति मेमोरी फ़ाइल का उपयोग कर सकते हैं, या आप जो भी कर सकते हैं (आसान तरीका) कर सकते हैं और vb.net सुविधाओं का लाभ उठा सकते हैं (विशेष रूप से, एक जो आपको एकल इंस्टेंस ऐप्स बनाने देता है और वर्तमान में चल रहे इंस्टेंस में एक विधि को कॉल करता है जो कमांड लाइन पर जाता है [इसलिए आप इसे अपने एप्लिकेशन में विधि का आह्वान करने के लिए उपयोग कर सकते हैं])। मुझे पता है कि सी # में वीबी कक्षाओं का उपयोग थोड़ा खराब लगता है लेकिन यह सबसे अमूर्त और आसान तरीका है। प्रासंगिक लेखों से लिंक - http://www.codeproject.com/KB/cs/CSSIApp.aspx, http://msdn.microsoft.com/en-us/magazine/cc163741.aspx

+0

यह तकनीक काम करती है ... और अच्छी तरह से काम करती है। मैंने Win7 ऐप बार पर पिन किए जाने पर अपने ऐप के 'हालिया' मेनू से चलने वाले उदाहरण में कॉल करने के लिए इसका उपयोग किया है। – AlfredBr

+0

हाँ, यह एक बहुत ही सरल लेकिन प्रभावी तरीका है, लेकिन कुछ लोगों (स्वयं सहित) के लिए वीबी कक्षाओं का उपयोग "= /" है – Zhanger

+3

मैं नामांकित पाइप का उपयोग करूंगा क्योंकि उन्हें किसी भी PInvoking की आवश्यकता नहीं है और बिना किसी अतिरिक्त संदर्भ के सी # में मूल रूप से चलाया जाता है। प्रिंसिपल उपरोक्त संदर्भित पहले कोडप्रोजेक्ट आलेख जैसा ही है, सिवाय इसके कि आप वास्तव में नामांकित पाइप के साथ कोड को व्यवस्थित कर सकते हैं। वह लेख 6 साल पुराना है ... – BenSwayne

6

का अंतिम भाग यहां लिखा गया एक छोटा सा सहायक है। किसी अन्य प्रक्रिया से

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 

:

इसके इस्तेमाल के

NamedPipeListener<String>.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(); 
      } 
     } 
    } 
}