Advertisement
Sta99ot

Untitled

Mar 30th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. using System.Drawing;
  8.  
  9. namespace SNMediaLib
  10. {
  11.  
  12.     public class ImageGeneratorBase
  13.     {
  14.         public int Threads { get; protected set; } = Environment.ProcessorCount;
  15.         Thread[] WorkerThreads;
  16.         Thread MonitorThread;
  17.         Semaphore[] Semaphores;
  18.         public bool Busy { get; set; } = false;
  19.         public int Width { get; protected set; }
  20.         public int Height { get; protected set; }
  21.         BitmapBuffer bitmap;
  22.         public object Parameter;
  23.  
  24.         public ImageGeneratorBase(int width,int height, int threads)
  25.         {
  26.             this.Width = width;
  27.             this.Height = height;
  28.             this.Threads = threads;
  29.         }
  30.  
  31.         public ImageGeneratorBase(int width, int height)
  32.         {
  33.             this.Width = width;
  34.             this.Height = height;
  35.             this.Threads = Environment.ProcessorCount;
  36.         }
  37.  
  38.         public double Progress
  39.         {
  40.             get { return Busy ? (double)((double)finishedLines / (double)bitmap.Height) : 1; }
  41.         }
  42.         int finishedLines = 0;
  43.         int totalLines;
  44.  
  45.         public event Action<BitmapBuffer> FinishedCallback;
  46.         public event Func<int, int, object, Color> PixelWorker;
  47.  
  48.         protected virtual Color  GetPixelColor(int x,int y, object param)
  49.         {
  50.             if (PixelWorker != null)
  51.                 return PixelWorker(x, y, param);
  52.             return Color.Black;
  53.         }
  54.  
  55.         void Worker(object number)
  56.         {
  57.             int threadNumber = (int)number;
  58.                
  59.                 for (int y = threadNumber; y < bitmap.Height; y += Threads)
  60.                 {
  61.                     for (int x = 0; x < bitmap.Width; x++)
  62.                     {
  63.                         bitmap[x, y] = GetPixelColor(x, y, Parameter);
  64.                     }
  65.                     finishedLines++;
  66.                 }
  67.             Semaphores[threadNumber].Release();
  68.         }
  69.  
  70.         void MonitorWorker()
  71.         {
  72.             WorkerThreads = new Thread[Threads];
  73.             Semaphores = new Semaphore[Threads];
  74.             for (int i = 0; i < Threads; i++)
  75.             {
  76.                 Semaphores[i] = new Semaphore(0, 1);
  77.                 WorkerThreads[i] = new Thread(new ParameterizedThreadStart(Worker));
  78.                 WorkerThreads[i].Start(i);
  79.             }
  80.             for (int i = 0; i < Threads; i++)
  81.             {
  82.                 Semaphores[i].WaitOne();
  83.             }
  84.             Busy = false;
  85.             FinishedCallback?.Invoke(bitmap);
  86.         }
  87.  
  88.         public void Draw(object param)
  89.         {
  90.             Busy = true;
  91.             finishedLines = 0;
  92.             bitmap = new BitmapBuffer(Width, Height);
  93.             MonitorThread = new Thread(MonitorWorker);
  94.             this.Parameter = param;
  95.             Thread mainThread = new Thread(MonitorWorker);
  96.             mainThread.Start();
  97.         }
  98.  
  99.         public void Abort()
  100.         {
  101.             if(Busy)
  102.             {
  103.                 MonitorThread.Abort();
  104.                 for (int i=0;i<Threads;i++)
  105.                 {
  106.                    WorkerThreads[i].Abort();
  107.                    Semaphores[i].Dispose();
  108.                 }
  109.                 WorkerThreads = null;
  110.                 Semaphores = null;
  111.                 MonitorThread = null;
  112.                 bitmap = null;
  113.                 Busy = false;
  114.             }
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement