public void GlobalAutoResetEvent_Basic() { #if WINFULL var thread = new Thread(new ThreadStart(ThreadProc)); #else var thread = new CEThread(new ThreadStart(ThreadProc)); #endif event1 = new GlobalAutoResetEvent("Test"); event2 = new GlobalAutoResetEvent("Test"); try { thread.Start(); Thread.Sleep(1100); event1.Set(); Thread.Sleep(1000); Assert.IsTrue(finishTime - startTime >= TimeSpan.FromMilliseconds(1000)); } finally { event1.Close(); event2.Close(); thread.Join(); } }
/// <summary> /// Releases any resources associated with the service control instance. /// </summary> public void Close() { id = Guid.Empty; isOpen = false; services = null; if (inbox != null) { inbox.Close(); inbox = null; } if (outbox != null) { outbox.Close(); outbox = null; } if (onReply != null) { onReply.Close(); onReply = null; } }
/// <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; } }