private static void startServer(string pn, frm_main f) { Decoder decoder = Encoding.Default.GetDecoder(); Byte[] bytes = new Byte[BufferSize]; char[] chars = new char[BufferSize]; int numBytes = 0; StringBuilder msg = new StringBuilder(); NamedPipeServerStream pipeServer; try { pipeServer = new NamedPipeServerStream(pn, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous); while (true) { pipeServer.WaitForConnection(); do { msg.Length = 0; do { numBytes = pipeServer.Read(bytes, 0, BufferSize); if (numBytes > 0) { int numChars = decoder.GetCharCount(bytes, 0, numBytes); decoder.GetChars(bytes, 0, numBytes, chars, 0, false); msg.Append(chars, 0, numChars); } } while (numBytes > 0 && !pipeServer.IsMessageComplete); decoder.Reset(); if (numBytes > 0) { //MESSAGE O string json = msg.ToString(); JSONobject a = JsonConvert.DeserializeObject<JSONobject>(json); switch (a.action) { case "dial": Debug.WriteLine("Call command from namedPipe..."); f.changeState("dialing"); f.setFocus(); f.setNumber(a.data.telnr); break; } //ownerInvoker.Invoke(msg.ToString()); } } while (numBytes != 0); pipeServer.Disconnect(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // get application GUID as defined in AssemblyInfo.cs // 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 JSONobject j = new JSONobject(); j.action = "dial"; j.data = new Data(); j.data.telnr = args[0].Remove(0, 4).Trim('/'); Pipe p = new Pipe("OTMPipe"); p.send(JsonConvert.SerializeObject(j)); Environment.Exit(0); } else { // handle protocol with this instance Program.f = new frm_main(); Application.Run(f); } } finally { mutex.ReleaseMutex(); } } }
public void hookForm(frm_main f) { this.form = f; }