Example #1
0
        private void IniciarBotao(object sender, EventArgs e)
        {
            Contador1.Text = "0";
            Contador2.Text = "0";
            Contador3.Text = "0";
            Contador4.Text = "0";
            Contador5.Text = "0";

            for (int i = 0; i < Garfo.Count(); i++)
            {
                Garfo[i].EmUso       = false;
                Filosofo[i].Pensando = false;
            }

            ControleThread = true;

            F1.Abort();
            F2.Abort();
            F3.Abort();
            F4.Abort();
            F5.Abort();

            { F1 = new Thread(Filosofo1); F1.Start(); }
            { F3 = new Thread(Filosofo3); F3.Start(); }
            { F5 = new Thread(Filosofo5); F5.Start(); }
            { F2 = new Thread(Filosofo2); F2.Start(); }
            { F4 = new Thread(Filosofo4); F4.Start(); }


            iniciar.Enabled   = false;
            Finalizar.Enabled = true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            count_f1.Text = "0";
            count_f2.Text = "0";
            count_f3.Text = "0";
            count_f4.Text = "0";
            count_f5.Text = "0";

            for (int i = 0; i < Garfo.Count(); i++)
            {
                Garfo[i].EstaEmUso        = false;
                Filosofos[i].EstaPensando = false;
            }

            controlaThreads = true;

            F1.Abort();
            F2.Abort();
            F3.Abort();
            F4.Abort();
            F5.Abort();

            { F1 = new Thread(Filosofo1); F1.Start(); }
            { F3 = new Thread(Filosofo3); F3.Start(); }
            { F5 = new Thread(Filosofo5); F5.Start(); }
            { F2 = new Thread(Filosofo2); F2.Start(); }
            { F4 = new Thread(Filosofo4); F4.Start(); }
            button1.Enabled = false;
            button2.Enabled = true;
        }
Example #3
0
        private void Filosofo5()
        {
            int tempo        = sorteio.Next(3000, 6000);
            int TempoComendo = tempo / Controlatempo;

            while (ControleThread)
            {
                if (Garfo[3].EmUso == true || Garfo[4].EmUso == true)
                {
                    this.BeginInvoke((MethodInvoker) delegate
                    {
                        ListaTela.Items.Add(Filosofo[4].Nome + " está pensando");
                        Status5.Text = "Pensando";
                    });

                    Thread.Sleep(2000);
                }
                else
                {
                    Semaforo.WaitOne();
                    {
                        Filosofo[4].Pensando = false;
                        Garfo[3].EmUso       = true;
                        Garfo[4].EmUso       = true;

                        this.BeginInvoke((MethodInvoker) delegate
                        {
                            ListaTela.Items.Add(Filosofo[4].Nome + " está  comendo");
                            Status5.Text = "Comendo";
                            ContadorFilosofo5++;

                            Contador5.Text = ContadorFilosofo5.ToString();
                        });


                        Thread.Sleep(TempoComendo);

                        this.BeginInvoke((MethodInvoker) delegate
                        {
                            ListaTela.Items.Add(Filosofo[4].Nome + " está  pensando");
                            Status5.Text = "Pensando";
                        });

                        Filosofo[4].Pensando = true;
                        Garfo[3].EmUso       = false;
                        Garfo[4].EmUso       = false;
                    }
                    Semaforo.Release();
                    if (ContadorFilosofo5 == 5)
                    {
                        F5.Abort();
                        return;
                    }
                }
            }
        }