Advertisement
Guest User

Wątki

a guest
Apr 27th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ThreadingLesson
  5. {
  6.     class ThreadExample
  7.     {
  8.         private int _counter;
  9.         private bool _exit;
  10.        
  11.         Thread countThread;
  12.         Thread displayThread;
  13.         Thread inputThread;
  14.        
  15.         Thread overloadThread;
  16.        
  17.         //Konstruktor
  18.         public ThreadExample()
  19.         {
  20.             _counter = 0;
  21.             _exit = false;
  22.            
  23.             //Inicjowanie wątków
  24.             countThread = new Thread(new ThreadStart(Counting));
  25.             displayThread = new Thread(new ThreadStart(Displaying));
  26.             inputThread = new Thread(new ThreadStart(InputReading));
  27.             overloadThread = new Thread(new ThreadStart(OverloadThread));
  28.         }
  29.        
  30.         public void RunThreads()
  31.         {
  32.             //Rozpoczęcie działania wątków
  33.             countThread.Start();
  34.             displayThread.Start();
  35.             inputThread.Start();
  36.            
  37.             //Uwaga, zwiększa użycie procesora do 100%
  38.             //overloadThread.Start();
  39.         }
  40.        
  41.         private void Counting()
  42.         {
  43.             while(!_exit)
  44.             {
  45.                 _counter++;
  46.                 Thread.Sleep(1000);
  47.             }
  48.         }
  49.        
  50.         private void Displaying()
  51.         {
  52.             while(!_exit)
  53.             {
  54.                 Console.SetCursorPosition(0, 0);
  55.                 Console.WriteLine("Stan licznika: " + _counter.ToString());
  56.             }
  57.         }
  58.        
  59.         private void InputReading()
  60.         {
  61.             //Niestandardowy for - zmienia wartość pause na przemian,
  62.             //działa dopóki !_exit, jak pozostałe
  63.             for(bool pause = false; !_exit; pause = !pause)
  64.             {
  65.                 Console.ReadLine();
  66.                 if(pause)
  67.                     countThread.Suspend();
  68.                 else
  69.                     countThread.Resume();
  70.             }
  71.         }
  72.        
  73.         //Metoda do zabawy w przeciążanie wątku
  74.         private void OverloadThread()
  75.         {
  76.             while(true)
  77.             {
  78.                 try
  79.                 {
  80.                     overloadThread = new Thread(new ThreadStart(OverloadThread));
  81.                     overloadThread.Start();
  82.                 }
  83.                 catch(Exception ex)
  84.                 {
  85.                     Console.WriteLine(ex);
  86.                 }
  87.             }
  88.         }
  89.     }
  90.    
  91.     public class Program
  92.     {
  93.         public static void Main(string[] args)
  94.         {
  95.             ThreadExample example = new ThreadExample();
  96.             example.RunThreads();
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement