Xom9ik

Lab_6/15var (IV semester) OS&SP

May 29th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Forms;
  4. using System.Windows.Forms.DataVisualization.Charting;
  5. using System.Threading;
  6. using System.Runtime.InteropServices;
  7. using System.Reflection;
  8.  
  9. namespace Lab6
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
  14.  
  15.         [StructLayout(LayoutKind.Sequential)]
  16.         public struct CRITICAL_SECTION { public int dummy; }
  17.        
  18.         [DllImport("kernel32.dll")]
  19.         static extern bool InitializeCriticalSectionAndSpinCount(ref CRITICAL_SECTION
  20.            lpCriticalSection, uint dwSpinCount);
  21.                
  22.         [DllImport("kernel32.dll")]
  23.         static extern void EnterCriticalSection(ref CRITICAL_SECTION
  24.            lpCriticalSection);
  25.        
  26.         [DllImport("kernel32.dll")]
  27.         static extern void LeaveCriticalSection(ref CRITICAL_SECTION
  28.            lpCriticalSection);
  29.  
  30.         CRITICAL_SECTION criticalSection = new CRITICAL_SECTION();
  31.  
  32.         static Series chartData = new Series("Sinus");
  33.         static Thread calcThread;
  34.         static Thread timeThread;
  35.         static System.Diagnostics.Stopwatch sw;
  36.         static double area = 0, areaAbs = 0, y, x;
  37.         static int counter;
  38.         static double timeLeft;
  39.         static double progress;
  40.         static double timeToFinish;
  41.         static int n;
  42.  
  43.         public Form1()
  44.         {
  45.             InitializeComponent();
  46.         }
  47.  
  48.         private void startButton_Click(object sender, EventArgs e)
  49.         {
  50.             Start();
  51.         }
  52.  
  53.         private async void Start()
  54.         {
  55.             await Task.Delay(1000);
  56.             sw = System.Diagnostics.Stopwatch.StartNew();
  57.             InitializeCriticalSectionAndSpinCount(ref criticalSection, 0);
  58.             calcThread = new Thread(() => CalcIntegral((new double[] { Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text), Convert.ToDouble(textBox3.Text) })));
  59.             calcThread.Priority = ThreadPriority.Lowest;
  60.             timeThread = new Thread(() => CalcTime());
  61.             timeThread.Priority = ThreadPriority.Lowest;
  62.             calcThread.Start();
  63.             timeThread.Start();
  64.             Thread.CurrentThread.Priority = ThreadPriority.Highest;
  65.         }
  66.  
  67.  
  68.         private void CalcTime()
  69.         {
  70.             while (n == 0){}
  71.             while (true)
  72.             {
  73.                 EnterCriticalSection(ref criticalSection);
  74.  
  75.                 progress = counter / n;
  76.                 timeLeft = Math.Round((double)sw.ElapsedMilliseconds / 1000, 3);
  77.                 timeToFinish = Math.Round((timeLeft / progress - timeLeft), 3);
  78.  
  79.                 LeaveCriticalSection(ref criticalSection);
  80.             }
  81.         }
  82.         void CalcIntegral(double[] args)
  83.         {
  84.             var dataArray = args;
  85.             double a = dataArray[0];
  86.             double b = dataArray[1];
  87.             n = Convert.ToInt32(dataArray[2]);
  88.             long prevElapsed = sw.ElapsedMilliseconds;
  89.             long deltaTime;
  90.             double h;
  91.             h = (b - a) / n;
  92.             for (counter = 0; counter < n; counter++)
  93.             {
  94.                 EnterCriticalSection(ref criticalSection);
  95.                 y = Math.Sin(x + 0.5 * h) + Math.Cos((x + 0.5 * h) * 0.9f);
  96.                 area += y * h;
  97.                 areaAbs += Math.Abs(y * h);
  98.                 deltaTime = sw.ElapsedTicks - prevElapsed;
  99.                 x += h;
  100.                 chartData.Points.AddXY(x, y);
  101.                 LeaveCriticalSection(ref criticalSection);
  102.             }
  103.             sw.Stop();
  104.         }
  105.  
  106.         void RefreshUI()
  107.         {
  108.             SetControlPropertyThreadSafe(stepsText, "Text", counter.ToString());
  109.             SetControlPropertyThreadSafe(xText, "Text", Math.Round(x, 5).ToString());
  110.             SetControlPropertyThreadSafe(yText, "Text", Math.Round(y, 5).ToString());
  111.             SetControlPropertyThreadSafe(progressBar1, "Value", (int)(100000 * progress));
  112.             SetControlPropertyThreadSafe(timeToFinishText, "Text", timeToFinish.ToString());
  113.             SetControlPropertyThreadSafe(timeText, "Text", timeLeft.ToString());          
  114.         }
  115.  
  116.         void StopButton_Click(object sender, EventArgs e)
  117.         {
  118.             calcThread.Abort();
  119.             timeThread.Abort();
  120.             sw.Stop();
  121.             RefreshUI();
  122.             chartData.ChartType = SeriesChartType.Line;
  123.             chart.Series.Add(chartData);
  124.         }
  125.  
  126.         public static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
  127.         {
  128.             if (control.InvokeRequired)
  129.             {
  130.                 control.Invoke(new SetControlPropertyThreadSafeDelegate
  131.                 (SetControlPropertyThreadSafe),
  132.                 new object[] { control, propertyName, propertyValue });
  133.             }
  134.             else
  135.             {
  136.                 control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[]
  137.                 {
  138.                     propertyValue
  139.                 });
  140.             }
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment