Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace Ospedale
- {
- /// <summary>
- /// Logica di interazione per MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- SortedSet<string> lista = new SortedSet<string>();
- static readonly Object locker = new object();
- static SemaphoreSlim semaforoPazienti = new SemaphoreSlim(0);
- Random rand = new Random();
- const int MAXTicket = 20;
- static int numProgrTicket = 0;
- static int[] ID = new int[6];
- public MainWindow()
- {
- InitializeComponent();
- Inizializza();
- Task.Factory.StartNew(() => AvviaSportello(borderSport1, tbSport1));
- Task.Factory.StartNew(() => AvviaSportello(borderSport2, tbSport2));
- Task.Factory.StartNew(() => AvviaSportello(borderSport3, tbSport3));
- }
- private void AvviaSportello(Border border, TextBlock textBlock)
- {
- while (true)
- {
- string stringa;
- int sleep;
- semaforoPazienti.Wait();
- lock (locker)
- {
- stringa = lista.Min;
- lista.Remove(stringa);
- sleep = rand.Next(4000, 8000 + 1);
- }
- Dispatcher.Invoke(() =>
- {
- border.Background = new SolidColorBrush(Colors.Orange);
- textBlock.Text = stringa;
- });
- Thread.Sleep(1000);
- bool trovato = false;
- int indexLista = 0;
- Dispatcher.Invoke(() =>
- {
- while (!trovato && indexLista < lstbPrenotazioni.Items.Count)
- {
- string s = lstbPrenotazioni.Items.GetItemAt(indexLista).ToString();
- if (s.Contains(stringa))
- {
- lstbPrenotazioni.Items.Remove(s);
- lstbPrenotazioni.Items.Insert(indexLista, s + " (in lavorazione)");
- trovato = !trovato;
- }
- else
- {
- indexLista++;
- }
- }
- });
- Dispatcher.Invoke(() =>
- {
- border.Background = new SolidColorBrush(Colors.Yellow);
- });
- Thread.Sleep(sleep);
- Dispatcher.Invoke(() =>
- {
- string stringaLista = lstbPrenotazioni.Items.GetItemAt(indexLista).ToString();
- lstbPrenotazioni.Items.Remove(stringaLista);
- stringaLista = stringaLista.Replace(" (in lavorazione)", "");
- lstbPrenotazioni.Items.Insert(indexLista, string.Format("{0} (completato)", stringaLista));
- });
- }
- }
- private void Inizializza()
- {
- Dispatcher.Invoke(() =>
- {
- Abutton.IsEnabled = true;
- Bbutton.IsEnabled = true;
- Cbutton.IsEnabled = true;
- Dbutton.IsEnabled = true;
- Ebutton.IsEnabled = true;
- Fbutton.IsEnabled = true;
- button.IsEnabled = false;
- lstbPrenotazioni.Items.Clear();
- tbSport1.Text = "---";
- tbSport2.Text = "---";
- tbSport3.Text = "---";
- });
- for(int i = 0; i < ID.Length; i++)
- {
- ID[i] = 0;
- }
- numProgrTicket = 0;
- lista.Clear();
- }
- private void Abutton_Click(object sender, RoutedEventArgs e)
- {
- Genera('A');
- }
- private void Bbutton_Click(object sender, RoutedEventArgs e)
- {
- Genera('B');
- }
- private void Cbutton_Click(object sender, RoutedEventArgs e)
- {
- Genera('C');
- }
- private void Dbutton_Click(object sender, RoutedEventArgs e)
- {
- Genera('D');
- }
- private void Ebutton_Click(object sender, RoutedEventArgs e)
- {
- Genera('E');
- }
- private void Fbutton_Click(object sender, RoutedEventArgs e)
- {
- Genera('F');
- }
- private void Genera(char lettera)
- {
- if (numProgrTicket < MAXTicket)
- {
- int posID = lettera - 'A';
- string ticketFormato;
- int IDTicket;
- lock (locker)
- {
- IDTicket = ++ID[posID];
- ticketFormato = string.Format("{0}{1:D2}", lettera, IDTicket);
- numProgrTicket++;
- lista.Add(ticketFormato);
- Dispatcher.Invoke(() =>
- {
- lstbPrenotazioni.Items.Add(string.Format("{0}. {1}", numProgrTicket, ticketFormato));
- });
- semaforoPazienti.Release();
- }
- }
- else
- {
- Dispatcher.Invoke(() =>
- {
- Abutton.IsEnabled = false;
- Bbutton.IsEnabled = false;
- Cbutton.IsEnabled = false;
- Dbutton.IsEnabled = false;
- Ebutton.IsEnabled = false;
- Fbutton.IsEnabled = false;
- button.IsEnabled = true;
- });
- }
- }
- private void button_Click(object sender, RoutedEventArgs e)
- {
- Inizializza();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement