2012-01-31 18 views
9

Sorunun tamamı URL Protokolleri ile ilgili.Başvuru olmadan URL Protokolü işleme yeniden başlatılıyor

Bir URL Protokolü mcm denilen kayıt yaptırdı, ama herhangi bir web tarayıcısından, bunun çalışması her şey fark, t uygulamanın yeni bir örneğini oluşturur. Zaten çalışan bir örnekte protokol isteğini işlemek için herhangi bir yolu var mı?

Örneğin, uTorrent, torrent protokolünü kullanırken, uygulamayı tekrar çalıştırmadan isteği hemen işleme alır. argümanları okumak için

private static void RegisterUrlProtocol() 
{ 
    UnregisterUrlProtocol(); 

    RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(UrlProtocol, true); 
    if (rKey == null) 
    { 
     rKey = Registry.ClassesRoot.CreateSubKey(UrlProtocol); 
     rKey.SetValue("", "URL: MazCraft Protocol"); 
     rKey.SetValue("URL Protocol", ""); 

     rKey = rKey.CreateSubKey(@"shell\open\command"); 
     rKey.SetValue("", "\"" + Application.ExecutablePath + "\" %1"); 
    } 

    if (rKey != null) 
    { 
     rKey.Close(); 
    } 
} 

Ve kod: Burada

Ben protokolü kayıt için kullandığınız koddur ... Ben gerçekten bu konuda ilginç bir şey bulamadı, bu yüzden burada soruyorum

private static bool CheckForProtocolMessage() 
{ 
    string[] arguments = Environment.GetCommandLineArgs(); 

    if (arguments.Length > 1) 
    { 
     string[] args = arguments[1].Split(':'); 
     args[1] = args[1].Replace("//", ""); 

     if (args[0].Trim().ToUpper() == "MCM" && args.Length > 1) 
     { 
      string[] actionDetail = args[1].Split('='); 

      if (actionDetail[0].Trim().ToUpper() == "INSTALL" && actionDetail.Length > 1) 
      { 
       string id = actionDetail[1].Trim().Replace("/", ""); 

       Funcs.ID = id; 

       return true; 
      } 
     } 
    } 

    return false; 
} 

Herhangi bir yardım büyük takdir :) Selamlamaları.

+5

onunla çıkacağını bakarsak eminim Bir utorrent yüklemesinin ikinci bir örneğini görmek, mevcut bir örneğini tespit etmek ve bu yolla iletişim kurmak PC yeni komut satırı –

+0

Ayrıca bkz http://stackoverflow.com/questions/917883/c-sharp-how-to-single-instance-application-that-accepts-new-parameters –

+0

Oh Aslında ne buldum aranan. Teşekkürler Alex :) –

cevap

4

Çalışmakta olan uygulamanın bir örneğini algılamak ve verileri Adlandırılmış Kanallar aracılığıyla varolan örneğe göndermek için bir Mutex kullanabilirsiniz.

Aşağıdaki örnekte yardım umuyoruz. İstediğiniz herhangi bir seri hale getirilebilir nesne için adlandırılmış yöneltme nesnesini (bu durumda dizgede) değiştirebilirsiniz.

NamedPipe.cs

namespace SingleInstanceNP 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.IO.Pipes; 
    using System.Runtime.Serialization.Formatters.Binary; 
    using System.Threading; 
    using System.IO; 


    public class NamedPipe<T> : IDisposable 
    { 
     #region Attribute and Properties 

     private string _pipeName; 
     private NamedPipeServerStream _pipeServer; 
     private bool _disposed; 
     private Thread _thread; 
     private bool _started; 

     #endregion 

     #region Constructors 

     public NamedPipe(NameTypes pipeType) 
     { 
      _disposed = false; 
      _started = false; 
      _pipeName = pipeType.ToString(); 
      _thread = new Thread(Main); 
      _thread.SetApartmentState(ApartmentState.STA); 
      _thread.Name = "NamePipe: " + pipeType.ToString() + " Thread"; 
      _thread.IsBackground = true; 
     } 

     ~NamedPipe() 
     { 
      Dispose(); 
     } 

     #endregion 

     #region Events 

     public delegate void Request(T t); 
     public event Request OnRequest; 

     #endregion 

     #region Public Methods 

     public static void Send(NameTypes pipeType, T t) 
     { 
      using (var npc = new NamedPipeClientStream(".", pipeType.ToString(), PipeDirection.Out)) 
      { 
       var bf = new BinaryFormatter(); 
       npc.Connect(); 
       bf.Serialize(npc, t); 
      } 
     } 

     public static T Recieve(NameTypes pipeType) 
     { 
      using (var nps = new NamedPipeServerStream(pipeType.ToString(), PipeDirection.In)) 
      { 
       return Recieve(nps); 
      } 
     } 

     public void Start() 
     { 
      if (!_disposed && !_started) 
      { 
       _started = true; 
       _thread.Start(); 
      } 
     } 

     public void Stop() 
     { 
      _started = false; 

      if (_pipeServer != null) 
      { 
       _pipeServer.Close(); 
       // disposing will occur on thread 
      } 
     } 

     public void Dispose() 
     { 
      _disposed = true; 
      Stop(); 

      if (OnRequest != null) 
       OnRequest = null; 
     } 

     #endregion 

     private void Main() 
     { 
      while (_started && !_disposed) 
      { 
       try 
       { 
        using (_pipeServer = new NamedPipeServerStream(_pipeName)) 
        { 
         T t = Recieve(_pipeServer); 

         if (OnRequest != null && _started) 
          OnRequest(t); 
        } 
       } 
       catch (ThreadAbortException) 
       { } 
       catch (System.IO.IOException iox) 
       { 
        Console.WriteLine("ERROR: {0}", iox.Message); 
        Thread.Sleep(TimeSpan.FromSeconds(30)); 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine("ERROR: {0}", ex.Message); 
        return; 
       } 
      } 
     } 

     private static T Recieve(NamedPipeServerStream nps) 
     { 
      var bf = new BinaryFormatter(); 

      try 
      { 
       nps.WaitForConnection(); 

       var obj = bf.Deserialize(nps); 

       if (obj is T) 
        return (T)obj; 
      } 
      // Catch the IOException that is raised if the pipe is 
      // broken or disconnected. 
      catch (IOException e) 
      { 
       Console.WriteLine("ERROR: {0}", e.Message); 
      } 
      return default(T); 
     } 

     #region Enums 

     public enum NameTypes 
     { 
      PipeType1 
     } 

     #endregion 
    } 
} 

program.cs What is a good pattern for using a Global Mutex in C#?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using System.Reflection; 
using System.Threading; 

namespace SingleInstanceNP 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      // get application GUID as defined in AssemblyInfo.cs 
      string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString(); 

      // unique id for global mutex - Global prefix means it is global to the machine 
      string mutexId = string.Format("Global\\{{{0}}}", appGuid); 

      using (var mutex = new Mutex(false, mutexId)) 
      { 
       try 
       { 
        if (!mutex.WaitOne(0, false)) 
        { 
         //signal existing app via named pipes 

         NamedPipe<string>.Send(NamedPipe<string>.NameTypes.PipeType1, "test"); 

         Environment.Exit(0); 
        } 
        else 
        { 
         // handle protocol with this instance 
         Application.Run(new Form1()); 

        } 
       } 
       finally 
       { 
        mutex.ReleaseMutex(); 
       } 
      } 
     } 
    } 
} 

Form1.cs APP GUID için kredi verin

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace SingleInstanceNP 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      // start listening for named pipe connections 
      var namedPipeString = new NamedPipe<string>(NamedPipe<string>.NameTypes.PipeType1); 
      namedPipeString.OnRequest += new NamedPipe<string>.Request(namedPipeString_OnRequest); 
      namedPipeString.Start(); 
     } 

     void namedPipeString_OnRequest(string t) 
     { 
      MessageBox.Show(t); 
     } 
    } 
} 
+1

-1: Bu tam bir cevap değil –

+0

Lütfen düzenlemeye bakın. İşe yardıma ihtiyacım olduğu için çalışma örneği verdim. Ayrıca @JohnSaunders Fark ettim "Herhangi bir yardım" kodu –

+0

olması gerekiyordu Şimdi biliyorsunuz: kod çok tercih edilir –

İlgili konular