Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Collections;
- using System.Collections.Generic;
- using Escolheimp;
- using System.Windows.Forms;
- namespace Impressora
- {
- public partial class Form1 : Form
- {
- Semaphore sem = new Semaphore(3, 3);
- delegate void SetTextCallback(string texto);
- delegate void SetTextCallback2(string texto);
- delegate void SetTextCallback3(string texto);
- Mutex mutex = new Mutex();
- string[] velocidade = new string[] { "NA", "NA", "NA" };
- string A;
- string B;
- string C;
- string[] estado= new string[] { "livre", "livre", "livre" };
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- for (int i = 0; i < 3; i++)
- {
- Thread newThread = new Thread(new ThreadStart(impr));
- newThread.Name = String.Format("Thread{0}", i + 1);
- newThread.Start();
- }
- }
- private void impr()
- {
- while (true)
- {
- sem.WaitOne();
- mutex.WaitOne();
- for (int i = 0; i < 3; i++)
- {
- if ((i==0)&&(this.estado[0] == "livre"))
- {
- this.estado[0] = "ocupado";
- //dictionary.Add("rapido", 0);
- this.B = Thread.CurrentThread.Name;
- velocidade[0] = this.B;
- break;
- }
- if ((i == 1) && (this.estado[1] == "livre"))
- {
- this.estado[1] = "ocupado";
- this.A = Thread.CurrentThread.Name;
- //dictionary.Add("medio", 1);
- velocidade[1] = this.A;
- break;
- }
- if ((i == 2) && (this.estado[2] == "livre"))
- {
- this.estado[2] = "ocupado";
- this.C = Thread.CurrentThread.Name;
- //dictionary.Add("lento", 2);
- this.velocidade[2] = this.C;
- break;
- }
- }
- mutex.ReleaseMutex();
- imprime(velocidade);
- mutex.WaitOne();
- librera(velocidade);
- mutex.ReleaseMutex();
- Thread.Sleep(200);
- sem.Release();
- }
- }
- private void librera (string[] spped)
- {
- string[] speed = spped;
- if (speed[0]==Thread.CurrentThread.Name)
- {
- this.estado[0] = "livre";
- this.velocidade[0] = "NA";
- //this.dictionary.Remove("rapido");
- }
- if (speed[1] == Thread.CurrentThread.Name)
- {
- this.estado[1] = "livre";
- this.velocidade[1] = "NA";
- //this.dictionary.Remove("medio");
- }
- if (speed[2] == Thread.CurrentThread.Name)
- {
- this.estado[2] = "livre";
- this.velocidade[2] = "NA";
- //this.dictionary.Remove("lento");
- }
- }
- private void imprime(string[] spped)
- {
- string[] speed = spped;
- if (speed[0] == Thread.CurrentThread.Name)
- {
- Thread.Sleep(500);
- if (this.txtTexto1.InvokeRequired)
- {
- SetTextCallback e = new SetTextCallback(attcont);
- this.Invoke(e, new object[] { Thread.CurrentThread.Name });
- }
- }
- if (speed[1] == Thread.CurrentThread.Name)
- {
- Thread.Sleep(600);
- if (this.txtTexto1.InvokeRequired)
- {
- SetTextCallback2 f = new SetTextCallback2(attcont1);
- this.Invoke(f, new object[] { Thread.CurrentThread.Name });
- }
- }
- if (speed[2] == Thread.CurrentThread.Name)
- {
- Thread.Sleep(700);
- if (this.txtTexto1.InvokeRequired)
- {
- SetTextCallback3 g = new SetTextCallback3(attcont2);
- this.Invoke(g, new object[] { Thread.CurrentThread.Name });
- }
- }
- }
- private void attcont(string texto )
- {
- this.txtTexto.Text += texto + "\n";
- }
- private void attcont1(string texto)
- {
- this.txtTexto1.Text += texto + "\n";
- }
- private void attcont2(string texto)
- {
- this.txtTexto2.Text += texto + "\n";
- }
- private void txtTexto_TextChanged_1(object sender, EventArgs e)
- {
- txtTexto.SelectionStart = txtTexto.Text.Length;
- txtTexto.ScrollToCaret();
- }
- private void txtTexto1_TextChanged(object sender, EventArgs e)
- {
- txtTexto1.SelectionStart = txtTexto1.Text.Length;
- txtTexto1.ScrollToCaret();
- }
- private void txtTexto2_TextChanged_1(object sender, EventArgs e)
- {
- txtTexto2.SelectionStart = txtTexto2.Text.Length;
- txtTexto2.ScrollToCaret();
- }
- private void label3_Click(object sender, EventArgs e)
- {
- }
- private void label2_Click(object sender, EventArgs e)
- {
- }
- private void label4_Click(object sender, EventArgs e)
- {
- }
- private void label4_Click_1(object sender, EventArgs e)
- {
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement