public void StopListening() { try { if (client == null) { return; } ReceivedCountEvent -= customEvent; IndividualCountersEvent -= individualEvent; customEvent = null; individualEvent = null; client = null; counterListenerTimer?.Cancel(); } catch (Exception e) { LogMessage($"{e.Message}"); } }
public void StartListening(ReceivedCount onReceivedCount = null, IndividualCounters onIndividualCounters = null) { if (client != null) { return; } client = new HttpClient { BaseAddress = new Uri(url) }; client.DefaultRequestHeaders .Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); customEvent = onReceivedCount; individualEvent = onIndividualCounters; if (onReceivedCount != null) { ReceivedCountEvent += onReceivedCount; } if (onIndividualCounters != null) { IndividualCountersEvent += onIndividualCounters; } // query the counter every pulseSec // and notify the listener try { counterListenerTimer = ThreadPoolTimer.CreatePeriodicTimer(BackgoundListen, timerPeriod); } catch (Exception e) { LogMessage($"{e.Message}"); } }