/// <summary> /// Readies the class to accept ServiceControl commands. /// </summary> /// <remarks> /// This must be called when the derived class is instantiated. /// </remarks> public void Open() { using (TimedLock.Lock(this)) { inbox = new SharedMemInbox(); inbox.Open(ServiceControl.ServiceMemPrefix + service.Name, ServiceControl.MaxMsgSize, new SharedMemInboxReceiveDelegate(OnReceive)); outbox = new SharedMemOutbox(ServiceControl.MaxMsgSize, ServiceControl.MaxWaitTime); } }
private void BasicThreadFunc() { SharedMemInbox inBox; DateTime start; inBox = new SharedMemInbox(); inBox.Open("BasicIn", 100, new SharedMemInboxReceiveDelegate(OnReceive)); ready = true; start = DateTime.UtcNow; while (recvMsg == null) { if (DateTime.UtcNow - start >= TimeSpan.FromSeconds(5)) { break; } Thread.Sleep(100); } inBox.Close(); }
/// <summary> /// Readies the service control instance for use. /// </summary> public void Open() { if (isOpen) { return; } // Load the configuration settings Config config = new Config(keyPrefix); string[] settings; int p, pEnd; string name, path, s, m; StartAs mode; settings = config.GetArray(setting); services = new ArrayList(settings.Length); for (int i = 0; i < settings.Length; i++) { s = settings[i]; try { p = 0; pEnd = s.IndexOf(';', p); if (pEnd == -1) { throw new Exception(); } name = s.Substring(0, pEnd - p).Trim(); p = pEnd + 1; if (string.IsNullOrWhiteSpace(name)) { throw new Exception(); } pEnd = s.IndexOf(';', p); if (pEnd == -1) { throw new Exception(); } path = s.Substring(p, pEnd - p).Trim(); p = pEnd + 1; m = s.Substring(p); switch (m.ToLowerInvariant()) { case "native": mode = StartAs.Native; break; case "form": mode = StartAs.Form; break; case "console": mode = StartAs.Console; break; default: throw new Exception(); } } catch { throw new FormatException(string.Format(null, "Invalid service configuration [{0}].", s)); } services.Add(new ServiceInfo(name, path, mode)); } // Initialize the shared memory and events necessary to communicate // with the services. id = Helper.NewGuid(); inbox = new SharedMemInbox(); inbox.Open(ControlMemPrefix + id.ToString(), ServiceControl.MaxMsgSize, new SharedMemInboxReceiveDelegate(OnReceive)); outbox = new SharedMemOutbox(MaxMsgSize, MaxWaitTime); onReply = new GlobalAutoResetEvent(null); isOpen = true; // Poll the services to find out which ones are already running. foreach (ServiceInfo info in services) { var state = this.GetStatus(info.Name); info.Started = state == ServiceState.Running || state == ServiceState.Starting; } }