private void RunTimer(int CodigoCliente) { try { ClienteMutexInfo StateObj = new ClienteMutexInfo(); StateObj.TimerCanceled = false; StateObj.SomeValue = 1; StateObj.IdCliente = CodigoCliente; StateObj.StatusProcessando = EnumProcessamento.LIVRE; StateObj.FirstTimeProcessed = DateTime.Now; System.Threading.TimerCallback TimerDelegate = new System.Threading.TimerCallback(TimerTask); gLogger.Debug("Inicia timer para cliente [" + CodigoCliente + "] com [" + IntervaloRecalculo + "] ms"); System.Threading.Timer TimerItem; TimerItem = new System.Threading.Timer(TimerDelegate, StateObj, 1000, IntervaloRecalculo); StateObj.TimerReference = TimerItem; if (htClientes.ContainsKey(CodigoCliente)) { htClientes.TryRemove(CodigoCliente, out StateObj); } htClientes.AddOrUpdate(CodigoCliente, StateObj, (key, oldValue) => StateObj); } catch (Exception ex) { gLogger.Info("Ocorreu um erro no método RunTimer ", ex); } }
private void RunTimer(int CodigoCliente) { try { ClienteMutexInfo StateObj = new ClienteMutexInfo(); StateObj.TimerCanceled = false; StateObj.SomeValue = 1; StateObj.IdCliente = CodigoCliente; StateObj.StatusProcessando = EnumProcessamento.LIVRE; System.Threading.TimerCallback TimerDelegate = new System.Threading.TimerCallback(TimerTask); gLogger.Debug("Inicia timer para cliente [" + CodigoCliente + "] com [" + IntervaloRecalculo + "] ms"); System.Threading.Timer TimerItem = new System.Threading.Timer(TimerDelegate, StateObj, 1000, IntervaloRecalculo); StateObj.TimerReference = TimerItem; } catch (Exception ex) { gLogger.Info("Ocorreu um erro no método RunTimer ", ex); } }
private void TimerTask(object StateObj) { gLogger.Debug("Entrando no timertask [" + ((ClienteMutexInfo)StateObj).IdCliente + "]"); try { ClienteMutexInfo State = (ClienteMutexInfo)StateObj; int Cliente = State.IdCliente; System.Threading.Interlocked.Increment(ref State.SomeValue); gLogger.Info("Thread disparada " + DateTime.Now.ToString() + " Cliente: " + State.IdCliente.ToString()); if (State.StatusProcessando == EnumProcessamento.LIVRE) { State.StatusProcessando = EnumProcessamento.EMPROCESSAMENTO; gLogger.Debug(" Cliente: " + State.IdCliente.ToString() + " - Aguardando Mutex"); State._Mutex.WaitOne(); gLogger.Info("INICIA CALCULO DE POSICAO " + DateTime.Now.ToString() + " Cliente: " + State.IdCliente.ToString()); lock (MonitorCustodiaMemoria) { MonitorCustodiaInfo info = this.CalcularPosicaoCustodia(Cliente); if (MonitorCustodiaMemoria.Contains(info.CodigoClienteBov)) { MonitorCustodiaMemoria[info.CodigoClienteBov] = info; } else { MonitorCustodiaMemoria.Add(info.CodigoClienteBov, info); } } gLogger.Info("POSICAO CALCULADA " + DateTime.Now.ToString() + " Cliente: " + State.IdCliente.ToString()); State._Mutex.ReleaseMutex(); State.StatusProcessando = EnumProcessamento.LIVRE; } else { gLogger.Info("processando " + DateTime.Now.ToString() + " Cliente: " + State.IdCliente.ToString()); } System.Threading.Timer myTimer = State.TimerReference; if (DateTime.Now.Hour <= 8 || DateTime.Now.Hour >= 20) { if (State.Intervalo != IntervaloRecalculoForaPregao) { myTimer.Change(0, IntervaloRecalculoForaPregao); State.Intervalo = IntervaloRecalculoForaPregao; gLogger.Info("Alterando intervalo de recalculo do cliente [" + Cliente + "] para " + IntervaloRecalculoForaPregao + "ms"); } } else { if (State.Intervalo != IntervaloRecalculo) { myTimer.Change(0, IntervaloRecalculo); State.Intervalo = IntervaloRecalculo; gLogger.Info("Alterando intervalo de recalculo do cliente [" + Cliente + "] para " + IntervaloRecalculo + "ms"); } } if (State.TimerCanceled) { State.TimerReference.Dispose(); gLogger.Info("Done " + DateTime.Now.ToString()); } } catch (Exception ex) { gLogger.Error("Ocorreu um erro ao processar método TimerTask {requisição de processamento da rotina CalcularPosicao} " + ex.Message, ex); } }
private void AddRemoveClientRunTimerProcessed(int IdCliente) { ClienteMutexInfo StateObj = new ClienteMutexInfo(); StateObj.TimerCanceled = false; StateObj.SomeValue = 1; StateObj.IdCliente = IdCliente; StateObj.StatusProcessando = EnumProcessamento.LIVRE; //StateObj.FirstTimeProcessed = DateTime.Now; StateObj.Intervalo = IntervaloRecalculo; if (htClientes.ContainsKey(IdCliente)) { var ClienteMutex = new ClienteMutexInfo(); ClienteMutex = htClientes[IdCliente] as ClienteMutexInfo; StateObj.SomeValue = ClienteMutex.SomeValue; StateObj.FirstTimeProcessed = ClienteMutex.FirstTimeProcessed; double lMinutes = (DateTime.Now - ClienteMutex.FirstTimeProcessed).TotalMinutes; gLogger.InfoFormat("FirstTimeProcessed {0} - Datetime. Now {1}", ClienteMutex.FirstTimeProcessed, DateTime.Now); gLogger.InfoFormat("Consulta do Cliente {1} - Ultimo acesso {0}", lMinutes, IdCliente); if (lMinutes > 3) { gLogger.InfoFormat("Já passou 3 minutos da ultima consulta do Cliente {1} - Ultimo acesso {0}", lMinutes, IdCliente); htClientes.TryRemove(IdCliente, out StateObj); StateObj.FirstTimeProcessed = DateTime.Now; if (ClientesMonitor.Contains(IdCliente)) { lock (ClientesMonitor) { ClientesMonitor.Remove(IdCliente); } } if (MonitorCustodiaMemoria.ContainsKey(IdCliente)) { var lInfo = new MonitorCustodiaInfo(); MonitorCustodiaMemoria.TryRemove(IdCliente, out lInfo); } } else { //StateObj.FirstTimeProcessed = DateTime.Now; htClientes.TryRemove(IdCliente, out StateObj); if (!ClientesMonitor.Contains(IdCliente)) { lock (ClientesMonitor) { ClientesMonitor.Add(IdCliente); } } } } else { StateObj.FirstTimeProcessed = DateTime.Now; } htClientes.AddOrUpdate(IdCliente, StateObj, (key, oldValue) => StateObj); }