private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { this.Enabled = false; if (timer.IsRunning) { timer.Stop(); } if (inPort.IsOpen) { try { inPort.Close(); } catch (Exception ex) { } } if (settingsProvider.Data.IsUseOutPort) { if ((outPort != null) && (outPort.IsOpen)) { try { outPort.Close(); } catch (Exception ex) { } } } }
private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { logger.TextAddedEvent -= loggerTextAddedEventHandler; logger.FinishLog(); timer.Tick -= timerTickHandler; if (timer.IsRunning) { timer.Stop(); } timer.Dispose(); if (gnssEmulatorPort != null) { if (gnssEmulatorPort.IsOpen) { gnssEmulatorPort.Close(); } gnssEmulatorPort.Dispose(); } if (gtrPort.IsOpen) { gtrPort.NewNMEAMessage -= gtrPortNewNMEAMessageEventHandler; gtrPort.PortError -= gtrPortErrorEventHandler; gtrPort.Close(); } }
// Detaches from events, stops, and disposes of the dump timer. Timers created // through the CreateDumpTimer method should be released through this method. private void ReleaseDumpTimer(PrecisionTimer dumpTimer) { if (dumpTimer != null) { dumpTimer.Tick -= DumpTimer_Tick; dumpTimer.Stop(); dumpTimer.Dispose(); } }
public void Close() { while (Interlocked.CompareExchange(ref portLock, 1, 0) != 0) { Thread.SpinWait(1); } node.Stop(); if (timer.IsRunning) { timer.Stop(); } port.Close(); Interlocked.Decrement(ref portLock); }
public static void Run() { PrecisionTimer timer = new PrecisionTimer(); Random rand = new Random(); int a, b, c = 0; timer.Start(); for (int i = 0; i < 100000; i++) { a = rand.Next(); b = rand.Next(); if (a > b) { c = a; b = c; } else { c = b; b = a; } } timer.Stop(); Console.WriteLine(c); Console.WriteLine(timer.ElapsedSeconds); timer.Reset(); timer.Start(); for (int i = 0; i < 100000; i++) { a = rand.Next(); b = rand.Next(); c = a > b ? a : b; b = a > b ? c : a; } timer.Stop(); Console.WriteLine(c); Console.WriteLine(timer.ElapsedSeconds); Console.ReadLine(); }
private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { logger.Write("Closing..."); logger.FinishLog(); if (timer.IsRunning) { timer.Stop(); } if (port.IsOpen) { try { port.Close(); } catch { } } }
private void Process_ACK(object[] parameters) { if (timer.IsRunning) { timer.Stop(); timCnt = 0; } ErrorIDs errorID = (ErrorIDs)((int)parameters[0]); if (errorID == ErrorIDs.LOC_ERR_NO_ERROR) { if (ACKReceived != null) { ACKReceived(this, new EventArgs()); } } }
protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { #region timer if (timer.IsRunning) { timer.Stop(); } timer.Dispose(); #endregion #region inPorts if (inPorts != null) { try { inPorts.Close(); } catch { } inPorts.Dispose(); } #endregion #region outPort if (outPort != null) { if (outPort.IsOpen) { try { outPort.Close(); } catch { } } outPort.Dispose(); } #endregion } disposed = true; } }
protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { if (timer.IsRunning) { timer.Stop(); } timer.Dispose(); if (inPort.IsOpen) { try { inPort.Close(); } catch { } } inPort.Dispose(); if (AUXGNSSUsed) { try { auxGNSSPort.Close(); } catch { } auxGNSSPort.Dispose(); } if (OutPortUsed) { if (outPort.IsOpen) { try { outPort.Close(); } catch { } } outPort.Dispose(); } } disposed = true; } }
private void UpdateFinished(string description) { action = UCNL_FW_ACTION.IDLE; InvokeWriteStateLbl("Idle"); InvokeSwitchProgressBarMode(ProgressBarStyle.Continuous); InvokeSetProgressBarValue(0); timer.Stop(); InvokeWriteLogString(string.Format("Firmware update finished: {0}\r\n", description)); if (this.InvokeRequired) { this.Invoke((MethodInvoker) delegate { SetCtrlState(true); }); } else { SetCtrlState(true); } InvokeCtrlEnabled(startUploadingBtn, true); }
public static void Run() { PrecisionTimer timer = new PrecisionTimer(); PagedArray <int> paged = new PagedArray <int>(2, 64); int[] normy = new int[2048]; normy[0] = 1; normy[15] = 1; paged[0] = 1; paged[15] = 1; for (int i = 0; i < 1; i++) { timer.Reset(); timer.Start(); Array.Copy(normy, 0, normy, 34, 16); timer.Stop(); } Console.WriteLine(timer.ElapsedSecondsF); paged[50] = 0; for (int i = 0; i < 1; i++) { timer.Reset(); timer.Start(); paged.CopyData(0, 16, 34); timer.Stop(); } Console.WriteLine(timer.ElapsedSecondsF); Console.ReadLine(); }
public static void Run() { PrecisionTimer timer = new PrecisionTimer(); //CompleteBinarySearchTree<int> test = new CompleteBinarySearchTree<int>(10, 1); int count = 16384; //timer.Start(); //for (int i = 0; i < count; i++) // test.Add(i, i); //timer.Stop(); double time = timer.ElapsedSeconds / count; int[] testArray = new int[count]; PagedArray <int> testPaged = new PagedArray <int>(14, 1); testPaged[int.MaxValue] = 1; int a = 0; IEnumerator <int> enumer = testPaged.GetEnumerator(); timer.Reset(); //Console.ReadLine(); timer.Start(); while (enumer.MoveNext()) //foreach (int i in testArray) { //timer.Stop(); //Console.WriteLine(i); //timer.Start(); a = enumer.Current; //a = i; } timer.Stop(); //Console.WriteLine(a); //Console.WriteLine(time); //Console.WriteLine(time * count); //Console.WriteLine(time / (1.0 / 60.0) * 100); Console.WriteLine(timer.ElapsedSeconds / count); Console.WriteLine(timer.ElapsedSeconds); Console.WriteLine(timer.ElapsedSeconds / (1.0 / 60.0) * 100); Console.WriteLine(1.0 / 60.0); Console.ReadLine(); }
private void timer_Tick(object sender, EventArgs e) { if (IsApproxEq(t, pTime, dt * 0.1)) { timer.Stop(); } else { t += dt; foreach (var item in speedFunctions) { speeds[item.Key] = item.Value(zcoordinates[item.Key]); zcoordinates[item.Key] += dt * direction * speeds[item.Key]; } if (IsApproxEq(t, pTime / 2.0, dt * 0.1)) { if (direction > 0) { direction = -1; foreach (var item in zcoordinates) { zmaxPoints[item.Key] = item.Value; } } } } if (this.InvokeRequired) { this.Invoke((MethodInvoker) delegate { this.Invalidate(); }); } else { this.Invalidate(); } if (IsSimulationStepEvent) { SimulationStepEvent.Rise(this, new EventArgs()); } }
public static void Run() { int threadCount = Environment.ProcessorCount; Thread[] threads = new Thread[threadCount]; Primes = new long[threadCount]; barrier = new Barrier(threadCount); threads[0] = Thread.CurrentThread; PrecisionTimer timer = new PrecisionTimer(); for (int i = 1; i < threadCount; i++) { threads[i] = new Thread(ThreadMethodBarrier); } timer.Reset(); timer.Start(); for (int j = 1; j < threadCount; j++) { threads[j].Start(j); } for (int i = 0; i < 1000; i++) { Primes[0] = FindPrimeNumber(10000); barrier.SignalAndWait(); } Running = false; timer.Stop(); Console.WriteLine("Barrier Time: " + (timer.ElapsedSecondsF / 1000)); Console.WriteLine("Primes Found"); for (int i = 0; i < threadCount; i++) { Console.WriteLine(Primes[i]); } Console.ReadLine(); }
// Detaches from events, stops, and disposes of the dump timer. Timers created // through the CreateDumpTimer method should be released through this method. private void ReleaseDumpTimer(PrecisionTimer dumpTimer) { if (dumpTimer != null) { dumpTimer.Tick -= DumpTimer_Tick; dumpTimer.Stop(); dumpTimer.Dispose(); } }
// This timer function is called every millisecond so that frames can be published at the exact desired time private void m_timer_Tick(object sender, EventArgs e) { // Slower systems or systems under stress may have trouble keeping up with a 1-ms timer, so // we only process this code if it's not already processing... if (Monitor.TryEnter(m_timerTickLock, 2)) { try { DateTime now = DateTime.UtcNow; int frameMilliseconds, milliseconds = now.Millisecond; long ticks = now.Ticks; bool releaseTimer = false, resync = false; // Make sure current time is reasonably close to current frame index if (Math.Abs(milliseconds - m_frameMilliseconds[m_lastFrameIndex]) > m_frameWindowSize) { m_lastFrameIndex = 0; } // See if it is time to publish for (int frameIndex = m_lastFrameIndex; frameIndex < m_frameMilliseconds.Length; frameIndex++) { frameMilliseconds = m_frameMilliseconds[frameIndex]; if (frameMilliseconds == milliseconds) { // See if system skipped a publication window if (m_lastFrameIndex != frameIndex) { // We monitor for missed windows in quick succession (within 1.5 seconds) if (ticks - m_lastMissedWindowTime > 15000000L) { // Threshold has passed since last missed window, so we reset counters m_lastMissedWindowTime = ticks; m_missedPublicationWindows = 0; } m_missedPublicationWindows++; // If the system is starting to skip publications it could need resynchronization, // so in this case we restart the high-resolution timer to get the timer started // closer to the top of the millisecond resync = (m_missedPublicationWindows > 4); } // Prepare index for next check, time moving forward m_lastFrameIndex = frameIndex + 1; if (m_lastFrameIndex >= m_frameMilliseconds.Length) { m_lastFrameIndex = 0; } if (resync) { if ((object)m_timer != null) { m_timer.Stop(); ThreadPool.QueueUserWorkItem(SynchronizeInputTimer); m_resynchronizations++; } } releaseTimer = true; break; } // If time has yet to pass, wait till the next tick if (frameMilliseconds > milliseconds) { break; } } if (releaseTimer) { // Baseline time-stamp to the top of the millisecond for frame publication m_lastFrameTime = ticks - ticks % Ticks.PerMillisecond; // Pulse all waiting threads toggling between ready handles if (m_useWaitHandleA) { m_frameWaitHandleB.Reset(); m_useWaitHandleA = false; m_frameWaitHandleA.Set(); } else { m_frameWaitHandleA.Reset(); m_useWaitHandleA = true; m_frameWaitHandleB.Set(); } } } catch (Exception ex) { if ((object)m_exceptionHandler != null) { m_exceptionHandler(new InvalidOperationException("Exception thrown by precision input timer: " + ex.Message, ex)); } } finally { Monitor.Exit(m_timerTickLock); } } }
private void OnAppStateChanged(AppState state) { switch (state) { case AppState.PORT_ABSENT: { portNameCbx.Enabled = false; openClosePortBtn.Enabled = false; deviceInfoGroup.Enabled = false; buoyAddressGroup.Enabled = false; statusLbl.Text = "No ports available"; break; } case AppState.PORT_OPENED: { portNameCbx.Enabled = false; openClosePortBtn.Text = "Close"; deviceInfoGroup.Enabled = false; buoyAddressGroup.Enabled = false; statusLbl.Text = "Port opened"; var message = NMEAParser.BuildProprietarySentence(ManufacturerCodes.TNT, "4", new object[] { LOC_DATA_INFO.LOC_DATA_DEVICE_INFO, 0 }); if (!TrySend(message)) { State = AppState.PORT_CLOSED; } else { State = AppState.DEV_INFO_QUERIED; } break; } case AppState.PORT_ERROR: { if (timer.IsRunning) { timer.Stop(); } statusLbl.Text = "Port error"; break; } case AppState.PORT_CLOSED: { if (timer.IsRunning) { timer.Stop(); } portNameCbx.Enabled = true; openClosePortBtn.Text = "Open"; openClosePortBtn.Enabled = true; deviceInfoGroup.Enabled = false; buoyAddressGroup.Enabled = false; statusLbl.Text = "Port closed"; break; } case AppState.DEV_INFO_QUERIED: { statusLbl.Text = "Device info queried..."; timer.Start(); break; } case AppState.DEV_INFO_RECEIVED: { if (timer.IsRunning) { timer.Stop(); } deviceInfoGroup.Enabled = true; statusLbl.Text = "Device info received"; string message = NMEAParser.BuildProprietarySentence(ManufacturerCodes.TNT, "7", new object[] { 0 }); if (TrySend(message)) { State = AppState.DEV_SETTINGS_QUERIED; timer.Start(); } else { State = AppState.PORT_ERROR; } break; } case AppState.DEV_TIMEOUT: { statusLbl.Text = "Device timeout"; break; } case AppState.WRONG_DEVICE: { if (timer.IsRunning) { timer.Stop(); } buoyAddressGroup.Enabled = false; statusLbl.Text = "Connected device is not a RedBASE"; break; } case AppState.DEV_SETTINGS_QUERIED: { statusLbl.Text = "Settings queried..."; break; } case AppState.DEV_SETTINGS_RECEIVED: { if (timer.IsRunning) { timer.Stop(); } buoyAddressGroup.Enabled = true; statusLbl.Text = "Settings received"; break; } case AppState.DEV_SETTINGS_APPLY_QUERIED: { statusLbl.Text = "Settings apply queried..."; timer.Start(); break; } case AppState.DEV_SETTINGS_UPDATED: { if (timer.IsRunning) { timer.Stop(); } statusLbl.Text = "Settings updated"; break; } default: { break; } } }
public static void Run(int entityCount, int componentCount) { PrecisionTimer timer = new PrecisionTimer(); List <int>[] componentLists = new List <int> [componentCount]; int[] requiredComponents = new int[componentCount]; Random rand = new Random(); for (int i = 0; i < componentCount; i++) { componentLists[i] = new List <int>(entityCount); requiredComponents[i] = i; } for (int i = 0; i < entityCount; i++) { for (int j = 0; j < componentCount; j++) { if (rand.Next(10) > 5) { componentLists[j].Add(i); } } } Console.WriteLine("Time to Create Entity : " + timer.ElapsedSeconds); timer.Reset(); List <int[]> components = new List <int[]>(); timer.Reset(); timer.Start(); int[] place = new int[requiredComponents.Length]; bool done = false; for (place[0] = 0; place[0] < componentLists[0].Count; place[0]++) { int count = 1; int val = componentLists[0][place[0]]; for (int i = 1; i < place.Length; i++) { while (componentLists[i][place[i]] < val) { place[i]++; if (place[i] == componentLists[i].Count) { done = true; break; } } if (done) { break; } if (componentLists[i][place[i]] > val) { break; } count++; } if (done) { break; } if (count == place.Length) { components.Add(place); } } timer.Stop(); Console.WriteLine("Time for Component Worst Case: " + timer.ElapsedSeconds); Console.ReadLine(); }
public void Close() { timer.Stop(); port.Close(); }
public static void Run() { PrecisionTimer timer = new PrecisionTimer(); Random rand = new Random(); int[] ID1 = new int[100000]; int[] ID2 = new int[1000]; Console.WriteLine("Set Equivalence Test"); for (int i = 0; i < ID1.Length; i++) { ID1[i] = rand.Next(); if (i < ID2.Length) { ID2[i] = ID1[i]; } } bool found = false; timer.Start(); for (int i = 0; i < ID1.Length; i++) { found = false; for (int j = 0; j < ID2.Length; j++) { if (ID1[i] == ID2[j]) { found = true; break; } } if (!found) { break; } } timer.Stop(); Console.WriteLine(found); Console.WriteLine(timer.ElapsedSeconds); timer.Reset(); timer.Start(); found = ID1.All((i) => { return(ID2.Contains(i)); }); timer.Stop(); Console.WriteLine(found); Console.WriteLine(timer.ElapsedSeconds); timer.Reset(); timer.Start(); found = ID1.All((i) => { return(ID2.Any((j) => { return i == j; })); }); timer.Stop(); Console.WriteLine(found); Console.WriteLine(timer.ElapsedSeconds); timer.Reset(); timer.Start(); //Fastest in worst case found = !ID1.Any((i) => { return(!ID2.Contains(i)); }); timer.Stop(); Console.WriteLine(found); Console.WriteLine(timer.ElapsedSeconds); timer.Reset(); for (int i = 0; i < ID2.Length; i++) { ID2[i] = rand.Next(); } timer.Start(); //fastest in best case for (int i = 0; i < ID1.Length; i++) { found = false; for (int j = 0; j < ID2.Length; j++) { if (ID1[i] == ID2[j]) { found = true; break; } } if (!found) { break; } } timer.Stop(); Console.WriteLine(found); Console.WriteLine(timer.ElapsedSeconds); timer.Reset(); timer.Start(); found = ID1.All((i) => { return(ID2.Contains(i)); }); timer.Stop(); double temp = timer.ElapsedSeconds; Console.WriteLine(found); Console.WriteLine(timer.ElapsedSeconds); timer.Reset(); timer.Start(); found = ID1.All((i) => { return(ID2.Any((j) => { return i == j; })); }); timer.Stop(); Console.WriteLine(found); Console.WriteLine(timer.ElapsedSeconds); timer.Reset(); // // // Best in Worst Case and Close 2nd in Best Case // // timer.Start(); found = !ID1.Any((i) => { return(!ID2.Contains(i)); }); timer.Stop(); Console.WriteLine(found); Console.WriteLine(timer.ElapsedSeconds); Console.WriteLine(timer.ElapsedSeconds / temp * 100); timer.Reset(); Console.ReadLine(); }
public void Stop() { timer.Stop(); }
void Run(SystemInfo[] systems, SystemType type, double time, int laneCount) { int typeCount = 0; double averageTime = 0; PrecisionTimer processTimer = new PrecisionTimer(); PrecisionTimer unitTimer = new PrecisionTimer(); Console.WriteLine("Test 1 - Adding All Systems at Once"); for (int i = 0; i < systems.Length; i++) { if (systems[i].Type == type) { typeCount++; } } SafeSystemSchedule schedule = new SafeSystemSchedule(SystemType.Logic, laneCount, typeCount); processTimer.Start(); for (int i = 0; i < systems.Length; i++) { unitTimer.Start(); SystemInfo sysInf = systems[i]; if (!sysInf.Active || sysInf.Type != type) { unitTimer.Stop(); averageTime = MathHelper.MovingAverage(averageTime, unitTimer.ElapsedSeconds, i + 1); unitTimer.Reset(); continue; } schedule.Add(sysInf); unitTimer.Stop(); processTimer.Stop(); averageTime = MathHelper.MovingAverage(averageTime, unitTimer.ElapsedSeconds, i + 1); unitTimer.Reset(); processTimer.Start(); } processTimer.Stop(); Console.WriteLine("Total Time to Register : " + processTimer.ElapsedSeconds); Console.WriteLine("Average Time to Register : " + averageTime); Console.WriteLine("Extrapolated Time to Register : " + (averageTime * systems.Length)); Console.WriteLine("Extrapolated Time without nonTypes : " + (averageTime * typeCount)); processTimer.Reset(); Console.ReadLine(); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Test 2 - Adding Each System One at a Time"); averageTime = 0; schedule.Clear(); processTimer.Start(); for (int i = 0; i < systems.Length; i++) { unitTimer.Start(); SystemInfo sysInf = systems[i]; if (!sysInf.Active || sysInf.Type != type) { unitTimer.Stop(); averageTime = MathHelper.MovingAverage(averageTime, unitTimer.ElapsedSeconds, i + 1); unitTimer.Reset(); continue; } schedule.Add(sysInf); unitTimer.Stop(); processTimer.Stop(); averageTime = MathHelper.MovingAverage(averageTime, unitTimer.ElapsedSeconds, i + 1); unitTimer.Reset(); while (schedule.NextSystem(0, out SystemInfo sysInfo) != ScheduleResult.Finished) { sysInfo.Age = 1; } processTimer.Start(); } processTimer.Stop(); Console.WriteLine("Total Time to Register : " + processTimer.ElapsedSeconds); Console.WriteLine("Average Time to Register : " + averageTime); Console.WriteLine("Extrapolated Time to Register : " + (averageTime * systems.Length)); Console.WriteLine("Extrapolated Time without nonTypes : " + (averageTime * typeCount)); processTimer.Reset(); Console.ReadLine(); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Test 3 - Clear List and Readd"); SafeSystemSchedule copy = new SafeSystemSchedule(schedule); foreach (SystemInfo sysinf in systems) { SystemUpdate(sysinf); } processTimer.Start(); schedule.Clear(); for (int i = 0; i < systems.Length; i++) { SystemInfo sysInf = systems[i]; if (!sysInf.Active || sysInf.Type != type) { continue; } schedule.Add(sysInf); } processTimer.Stop(); while (schedule.NextSystem(0, out SystemInfo sysInfo) != ScheduleResult.Finished) { Console.WriteLine(sysInfo); } Console.WriteLine("Total Time to clear then readd : " + processTimer.ElapsedSeconds); Console.ReadLine(); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Test 4 - Remove Individually and Register One at a time"); processTimer.Start(); copy.Reset(); for (int i = 0; i < systems.Length; i++) { SystemInfo sysInf = systems[i]; if (!sysInf.Active || sysInf.Type != type) { continue; } copy.Remove(sysInf); copy.Add(sysInf); } processTimer.Stop(); while (copy.NextSystem(0, out SystemInfo sysInfo) != ScheduleResult.Finished) { Console.WriteLine(sysInfo); } Console.WriteLine("Total Time to remove then readd : " + processTimer.ElapsedSeconds); Console.ReadLine(); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Test 5 - System Run"); schedule.Clear(); for (int i = 0; i < systems.Length; i++) { SystemInfo sysInf = systems[i]; if (!sysInf.Active || sysInf.Type != type) { continue; } schedule.Add(sysInf); } double currentTime = 0; int finishCount = 0; double[] endTimes = new double[laneCount]; bool[] finished = new bool[laneCount]; averageTime = 0; processTimer.Start(); while (!schedule.Finished && currentTime < time && finishCount < laneCount) { for (int i = 0; i < laneCount; i++) { if (endTimes[i] <= currentTime && !finished[i]) { unitTimer.Start(); schedule.FinishLane(i); ScheduleResult result = schedule.NextSystem(i, out SystemInfo sysInf); if (result == ScheduleResult.Supplied) { sysInf.Age = 0; if (sysInf.AverageRunTime + currentTime > time) { finished[i] = true; finishCount++; unitTimer.Stop(); continue; } endTimes[i] = currentTime + sysInf.AverageRunTime; } else if (result == ScheduleResult.Finished) { finished[i] = true; finishCount++; } unitTimer.Stop(); processTimer.Stop(); averageTime = MathHelper.MovingAverage(averageTime, unitTimer.ElapsedSeconds, i + 1); unitTimer.Reset(); processTimer.Start(); } } processTimer.Stop(); double nextTime = double.PositiveInfinity; for (int i = 0; i < laneCount; i++) { if (endTimes[i] > currentTime && endTimes[i] < nextTime) { nextTime = endTimes[i]; } } if (nextTime != double.PositiveInfinity) { currentTime = nextTime; } processTimer.Start(); } processTimer.Stop(); Console.WriteLine("Total Time to decide : " + processTimer.ElapsedSeconds); Console.WriteLine("Average Time to decide : " + averageTime); Console.WriteLine("Extrapolated Time to decide : " + (averageTime * typeCount)); Console.WriteLine("Total Time Used : " + (currentTime) + "/" + time); processTimer.Reset(); Console.ReadLine(); }