private void timerCallback(object state) { ForceValueUpdatedSynchronizationContext.Post(async(chatTrendService) => { IChatTrendService service = (IChatTrendService)chatTrendService; IForceValueData forceValueData; try { forceValueData = await service.GetForceValueData(); } catch (ChatTrendServiceException) { RemoveService(service); //TODO: サービスが消えたことを伝える return; } var item = forceValues.FirstOrDefault(x => x.Item1 == chatTrendService); if (item != null) { forceValues.Remove(item); } forceValues.Add(new Tuple <IChatTrendService, IForceValueData>(service, forceValueData)); ForceValueUpdated?.Invoke(this, new ForceValueUpdatedEventArgs(service, forceValueData)); }, state); }
public async void AddService(IChatTrendServiceEntry serviceEntry) { IChatTrendService service = await serviceEntry.GetNewService(); timers.TryAdd(service, new Timer(timerCallback, service, 1000, (int)service.UpdateInterval.TotalMilliseconds)); registeredServices.Add(service); }
public void RemoveService(IChatTrendService service) { registeredServices.Remove(service); Timer timer; timers.TryRemove(service, out timer); disposeService(service, timer); }
private void disposeService(IChatTrendService chatTrendService, Timer timer) { ManualResetEvent waitHandle = new ManualResetEvent(false); timer.Dispose(waitHandle); //Timer破棄 waitHandle.WaitOne(); chatTrendService.Dispose(); //ChatTrendService破棄 }
public ForceValueUpdatedEventArgs(IChatTrendService chatTrendService, IForceValueData forceValueData) { this.ChatTrendService = chatTrendService; this.ForceValueData = forceValueData; }