public static void Thread_Execute_TenSeconds() { DateTime start = DateTime.UtcNow; Console.WriteLine($"Thread 2 starting @ {start}."); try { /////////////////////////////////////////// // this WILL THROW a ConstraintException // /////////////////////////////////////////// // install the execution constraint by specifying a timeout in milliseconds ExecutionConstraint.Install((int)new TimeSpan(0, 0, 5).TotalMilliseconds, 0); for (int i = 0; i < 100; i++) { Thread.Sleep(100); } ExecutionConstraint.Install(-1, 0); } catch (Exception ex) { Console.WriteLine(">>> As the prophecy foretold: a ConstraintException was thrown!"); } var end = DateTime.UtcNow - start; Console.WriteLine($"Thread 2 end after {end.TotalMilliseconds.ToString("N0")} milliseconds."); }
private void PublishData(string topic, byte[] data) { Thread.Sleep((int)mqttPrePublishDelay); try { ExecutionConstraint.Install(20000, 0); while (mqtt == null || !mqtt.IsConnected || networkChanged) { networkChanged = false; ReconnectMqtt(); } mqtt.Publish(topic, data); } catch (ConstraintException) { networkChanged = true; errorCount++; } catch (Exception ex) { Debug.Print(ex.Message); networkChanged = true; errorCount++; } finally { ExecutionConstraint.Install(-1, 0); Thread.Sleep((int)mqttPostPublishDelay); } }
public static void Main() { const int timeout = 100; // 100 ms = maximum accepted duration of operation ExecutionConstraint.Install(timeout, 0); //install to check constraint //do something which must take less than timeout Thread.Sleep(50); //operation is executed in less time //end of operation ExecutionConstraint.Install(-1, 0); //uninstall Debug.Print("First operation successfully executed."); ExecutionConstraint.Install(timeout, 0); //install to check constraint //do something which must take less than timeout Thread.Sleep(150); //operation takes longer as forced, so an exception will be thrown after timeout ms //end of operation ExecutionConstraint.Install(-1, 0); //uninstall Debug.Print("Second operation successfully executed."); }
private void PublishData(string topic, byte[] data) { Util.Delay((int)mqttPrePublishDelay); #if MF_FRAMEWORK_VERSION_V4_3 try { ExecutionConstraint.Install(20000, 0); while (mqtt == null || !mqtt.IsConnected || networkChanged) { networkChanged = false; ReconnectMqtt(); } mqtt.Publish(topic, data); } catch (ConstraintException) { networkChanged = true; errorCount++; } catch (Exception ex) { Debug.Print(ex.Message); networkChanged = true; errorCount++; } finally { ExecutionConstraint.Install(-1, 0); Util.Delay((int)mqttPostPublishDelay); } #else try { while (mqtt == null || !mqtt.IsConnected || networkChanged) { networkChanged = false; ReconnectMqtt(); } mqtt.Publish(topic, data); } catch (Exception) { networkChanged = true; errorCount++; } #endif }
public static void Thread_Execute_TwoSeconds() { DateTime start = DateTime.UtcNow; Console.WriteLine($"Thread 1 starting @ {start}."); // install the execution constraint by specifying a timeout in milliseconds ExecutionConstraint.Install((int)new TimeSpan(0, 0, 5).TotalMilliseconds, 0); for (int i = 0; i < 20; i++) { Thread.Sleep(100); } // remove the execution constraint by calling the method with -1 ExecutionConstraint.Install(-1, 0); var end = DateTime.UtcNow - start; Console.WriteLine($"Thread 1 end after {end.TotalMilliseconds.ToString("N0")} milliseconds."); }
/// <summary> /// The RebootDevice method enables the caller to force a soft or hard reboot of the device. /// This method raises the OnRebootEvent. /// </summary> /// <param name="soft">Determines whether the reboot request is for a soft or hard reboot. Note, /// some devices may not support soft reboot.</param> /// <param name="exeConstraintTimeout_ms">Execution constraint timeout (in milliseconds) for /// the event handlers. If the event handlers take longer than the given value, then /// the handlers will be aborted and the reboot will be executed. /// </param> public static void RebootDevice(bool soft, int exeConstraintTimeout_ms) { try { ExecutionConstraint.Install(exeConstraintTimeout_ms, 4); RebootEventHandler h = OnRebootEvent; if (h != null) { h(soft); } } catch { } finally { Reboot(soft); } }
private void InitSensor() { try { ExecutionConstraint.Install(2000, 0); // It should not take more than 1 second (using 2 to be safe) for the sensor to stabilize per datasheet. while (_sensorInitialized == false) // Keep reading until the Read() method returns true or and we get a ConstraintException { if (Read(false)) { _sensorInitialized = true; } } } catch (ConstraintException) { throw new DeviceInitialisationException("RHT03 Sensor failed to initialize."); } finally { ExecutionConstraint.Install(-1, 0); } }