public virtual void soltarComensal(int idFilosofo, CLog log) { lock (this) { comensal++; if (CForm.gettxtLog() != null) { log.doRegistros(" El Filosofo " + (idFilosofo + 1) + " ya NO es el comensal " + comensal); } Monitor.Pulse(this); } }
public void soltarPalillo(int idFilosofo, CLog log) { lock (locker) { Libre = true; if (CForm.gettxtLog() != null) { log.doRegistros(" El Filosofo " + (idFilosofo + 1) + " suelta el palillo " + (idPalillo + 1)); } Monitor.Pulse(locker); } }
public void tomarPalillo(int idFilosofo, CLog log) { lock (locker) { while (!Libre) { Monitor.Wait(locker); } if (CForm.gettxtLog() != null) { log.doRegistros(" El Filosofo " + (idFilosofo + 1) + " toma el palillo " + (idPalillo + 1)); } Libre = false; } }
public virtual void tomarComensal(int idFilosofo, CLog log) { lock (this) { while (comensal == 0) { Monitor.Wait(this); } if (CForm.gettxtLog() != null) { log.doRegistros(" El Filosofo " + (idFilosofo + 1) + " es el comensal " + comensal); } comensal--; } }
public void uncaughtException(Thread t, Exception e) { MessageBox.Show("Thread que lanzo la excepcion: {0} \n", t.Name); if (CForm.gettxtLog() != null) { try { log.doRegistros("\n Thread que lanzo la excepcion: " + t.Name + "\n"); log.doRegistros(e.ToString() + "\n\n"); } catch (ThreadInterruptedException ex) { MessageBox.Show(ex.ToString()); } } }
public bool tomarPalilloIzq(int idFilosofo, CLog log) { lock (locker) { while (!Libre) { Monitor.Wait(locker, r.Next(1000) + 500); return(false); } if (CForm.gettxtLog() != null) { log.doRegistros(" El Filosofo " + (idFilosofo + 1) + " tomo el palillo " + (idPalillo + 1)); } Libre = false; return(true); } }
public void Run() { lock (locker) { while (true) { //Monitor.Enter(locker); try { comensal.tomarComensal(idFilosofo, log); this.label_F.BackColor = Color.Pink; der.tomarPalillo(idFilosofo, log); this.label_F.BackColor = Color.Cyan; this.label_P_der.BackColor = Color.Blue; if (!izq.tomarPalilloIzq(idFilosofo, log)) { if (CForm.gettxtLog() != null) { log.doRegistros(" El Filosofo " + (idFilosofo + 1) + " tendra que soltar el palillo " + (idFilosofo + 1) + " por exceso de tiempo y salir a pensar. "); } der.soltarPalillo(idFilosofo, log); this.label_P_der.BackColor = Color.Red; comensal.soltarComensal(idFilosofo, log); if (CForm.gettxtLog() != null) { log.doRegistros(" El Filosofo " + (idFilosofo + 1) + " esta pensando. "); } try { //Thread.Sleep(1000 + 100); Monitor.Wait(locker, r.Next(1000) + 100); } catch (ThreadInterruptedException ex) { if (CForm.gettxtLog() != null) { log.doRegistros("\n\n Error. Descripcion: " + ex.ToString() + "\n\n"); } } //Monitor.Pulse(locker); continue; } this.label_P_izq.BackColor = Color.Blue; this.label_F.BackColor = Color.Orange; this.label_F.ForeColor = Color.Blue; if (txt_C.InvokeRequired) { txt_C.Invoke(new Action(() => this.txt_C.Text = (" " + (++CForm.F_Conteo[idFilosofo])))); } //txt_C.Text = (" " + (++CForm.F_Conteo[idFilosofo])); if (CForm.gettxtLog() != null) { log.doRegistros(" El Filosofo " + (idFilosofo + 1) + " esta comiendo. "); } try { Thread.Sleep(1000 + 500); //Monitor.Wait(locker, r.Next(1000 + 500)); } catch (ThreadInterruptedException ex) { if (CForm.gettxtLog() != null) { log.doRegistros("\n\n Error. Descripcion: " + ex.ToString() + "\n\n"); } } this.label_F.BackColor = Color.Green; this.label_F.ForeColor = Color.Black; izq.soltarPalillo(idFilosofo, log); this.label_P_izq.BackColor = Color.LightGray; der.soltarPalillo(idFilosofo, log); this.label_P_der.BackColor = Color.LightGray; comensal.soltarComensal(idFilosofo, log); if (CForm.gettxtLog() != null) { log.doRegistros(" El Filosofo " + (idFilosofo + 1) + " esta pensando. "); } try { //Thread.Sleep(1000 + 100); Monitor.Wait(locker, r.Next(1000) + 100); } catch (ThreadInterruptedException ex) { if (CForm.gettxtLog() != null) { log.doRegistros("\n\n Error. Descripcion: " + ex.ToString() + "\n\n"); } } } catch (ThreadInterruptedException ex) { if (CForm.gettxtLog() != null) { log.doRegistros("\n\n Error. Descripcion: " + ex.ToString() + "\n\n"); } } if (finalizado) { break; } } } try { if (CForm.gettxtLog() != null) { log.doRegistros(" La cena ha terminado para todos: El Filosofo " + (idFilosofo + 1) + " se ha puesto a pensar. \n\nPulsar INICIAR para continuar. \n\n"); } } catch (ThreadInterruptedException ex) { if (CForm.gettxtLog() != null) { log.doRegistros("\n\n Error. Descripcion: " + ex.ToString() + "\n\n"); } } }