Advertisement
New_T

Impressora C#

Jun 5th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Threading;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using Escolheimp;
  13. using System.Windows.Forms;
  14.  
  15. namespace Impressora
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         Semaphore sem = new Semaphore(3, 3);
  20.         delegate void SetTextCallback(string texto);
  21.         delegate void SetTextCallback2(string texto);
  22.         delegate void SetTextCallback3(string texto);
  23.         Mutex mutex = new Mutex();
  24.         string[] velocidade = new string[] { "NA", "NA", "NA" };
  25.         string A;
  26.         string B;
  27.         string C;
  28.         string[] estado= new string[] { "livre", "livre", "livre" };
  29.  
  30.         public Form1()
  31.         {
  32.             InitializeComponent();
  33.         }
  34.  
  35.         private void button1_Click(object sender, EventArgs e)
  36.         {
  37.            
  38.  
  39.             for (int i = 0; i < 3; i++)
  40.             {
  41.                 Thread newThread = new Thread(new ThreadStart(impr));
  42.                 newThread.Name = String.Format("Thread{0}", i + 1);
  43.                 newThread.Start();
  44.             }
  45.         }
  46.  
  47.         private void impr()
  48.         {
  49.             while (true)
  50.             {
  51.                 sem.WaitOne();
  52.  
  53.                 mutex.WaitOne();
  54.                 for (int i = 0; i < 3; i++)
  55.                 {
  56.                     if ((i==0)&&(this.estado[0] == "livre"))
  57.                     {
  58.  
  59.                         this.estado[0] = "ocupado";
  60.                         //dictionary.Add("rapido", 0);
  61.                         this.B = Thread.CurrentThread.Name;
  62.                         velocidade[0] = this.B;
  63.                         break;
  64.  
  65.                     }
  66.                     if ((i == 1) && (this.estado[1] == "livre"))
  67.                     {
  68.  
  69.                         this.estado[1] = "ocupado";
  70.                         this.A = Thread.CurrentThread.Name;
  71.                         //dictionary.Add("medio", 1);
  72.                         velocidade[1] = this.A;
  73.                         break;
  74.  
  75.                     }
  76.                     if ((i == 2) && (this.estado[2] == "livre"))
  77.                     {
  78.  
  79.                         this.estado[2] = "ocupado";
  80.                         this.C = Thread.CurrentThread.Name;
  81.                         //dictionary.Add("lento", 2);
  82.                         this.velocidade[2] = this.C;
  83.                         break;
  84.  
  85.                     }
  86.  
  87.                 }
  88.  
  89.                
  90.                 mutex.ReleaseMutex();
  91.  
  92.                
  93.                 imprime(velocidade);
  94.            
  95.                 mutex.WaitOne();
  96.                 librera(velocidade);
  97.                 mutex.ReleaseMutex();
  98.                 Thread.Sleep(200);
  99.                 sem.Release();
  100.                
  101.             }
  102.         }
  103.  
  104.         private void librera (string[] spped)
  105.         {
  106.             string[] speed = spped;
  107.            
  108.             if (speed[0]==Thread.CurrentThread.Name)
  109.             {
  110.                 this.estado[0] = "livre";
  111.                 this.velocidade[0] = "NA";
  112.                 //this.dictionary.Remove("rapido");
  113.             }
  114.             if (speed[1] == Thread.CurrentThread.Name)
  115.             {
  116.                 this.estado[1] = "livre";
  117.                 this.velocidade[1] = "NA";
  118.                 //this.dictionary.Remove("medio");
  119.             }
  120.             if (speed[2] == Thread.CurrentThread.Name)
  121.             {
  122.                 this.estado[2] = "livre";
  123.                 this.velocidade[2] = "NA";
  124.                 //this.dictionary.Remove("lento");
  125.             }
  126.         }
  127.         private void imprime(string[] spped)
  128.         {
  129.             string[] speed = spped;
  130.             if (speed[0] == Thread.CurrentThread.Name)
  131.             {
  132.                 Thread.Sleep(500);
  133.                 if (this.txtTexto1.InvokeRequired)
  134.                 {
  135.                     SetTextCallback e = new SetTextCallback(attcont);
  136.                     this.Invoke(e, new object[] { Thread.CurrentThread.Name });
  137.  
  138.                 }
  139.                
  140.             }
  141.             if (speed[1] == Thread.CurrentThread.Name)
  142.             {
  143.                 Thread.Sleep(600);
  144.                 if (this.txtTexto1.InvokeRequired)
  145.                 {
  146.                     SetTextCallback2 f = new SetTextCallback2(attcont1);
  147.                     this.Invoke(f, new object[] { Thread.CurrentThread.Name });
  148.  
  149.                 }
  150.                
  151.             }
  152.             if (speed[2] == Thread.CurrentThread.Name)
  153.             {
  154.                 Thread.Sleep(700);
  155.                 if (this.txtTexto1.InvokeRequired)
  156.                 {
  157.                     SetTextCallback3 g = new SetTextCallback3(attcont2);
  158.                     this.Invoke(g, new object[] { Thread.CurrentThread.Name });
  159.  
  160.                 }
  161.                
  162.             }
  163.         }
  164.            
  165.  
  166.         private void attcont(string texto )
  167.         {
  168.            
  169.             this.txtTexto.Text += texto + "\n";
  170.            
  171.         }
  172.         private void attcont1(string texto)
  173.         {
  174.            
  175.             this.txtTexto1.Text += texto + "\n";
  176.            
  177.         }
  178.         private void attcont2(string texto)
  179.         {
  180.            
  181.             this.txtTexto2.Text += texto + "\n";
  182.            
  183.         }
  184.  
  185.  
  186.  
  187.        
  188.  
  189.        
  190.  
  191.         private void txtTexto_TextChanged_1(object sender, EventArgs e)
  192.         {
  193.             txtTexto.SelectionStart = txtTexto.Text.Length;
  194.             txtTexto.ScrollToCaret();
  195.         }
  196.  
  197.         private void txtTexto1_TextChanged(object sender, EventArgs e)
  198.         {
  199.             txtTexto1.SelectionStart = txtTexto1.Text.Length;
  200.             txtTexto1.ScrollToCaret();
  201.         }
  202.  
  203.         private void txtTexto2_TextChanged_1(object sender, EventArgs e)
  204.         {
  205.             txtTexto2.SelectionStart = txtTexto2.Text.Length;
  206.             txtTexto2.ScrollToCaret();
  207.         }
  208.  
  209.         private void label3_Click(object sender, EventArgs e)
  210.         {
  211.  
  212.         }
  213.  
  214.         private void label2_Click(object sender, EventArgs e)
  215.         {
  216.  
  217.         }
  218.  
  219.         private void label4_Click(object sender, EventArgs e)
  220.         {
  221.  
  222.         }
  223.  
  224.         private void label4_Click_1(object sender, EventArgs e)
  225.         {
  226.  
  227.         }
  228.  
  229.         private void Form1_Load(object sender, EventArgs e)
  230.         {
  231.  
  232.         }
  233.     }
  234.    
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement