Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Windows.Forms.DataVisualization.Charting;
- using System.Threading;
- using System.Runtime.InteropServices;
- using System.Reflection;
- namespace Lab6
- {
- public partial class Form1 : Form
- {
- private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
- [StructLayout(LayoutKind.Sequential)]
- public struct CRITICAL_SECTION { public int dummy; }
- [DllImport("kernel32.dll")]
- static extern bool InitializeCriticalSectionAndSpinCount(ref CRITICAL_SECTION
- lpCriticalSection, uint dwSpinCount);
- [DllImport("kernel32.dll")]
- static extern void EnterCriticalSection(ref CRITICAL_SECTION
- lpCriticalSection);
- [DllImport("kernel32.dll")]
- static extern void LeaveCriticalSection(ref CRITICAL_SECTION
- lpCriticalSection);
- CRITICAL_SECTION criticalSection = new CRITICAL_SECTION();
- static Series chartData = new Series("Sinus");
- static Thread calcThread;
- static Thread timeThread;
- static System.Diagnostics.Stopwatch sw;
- static double area = 0, areaAbs = 0, y, x;
- static int counter;
- static double timeLeft;
- static double progress;
- static double timeToFinish;
- static int n;
- public Form1()
- {
- InitializeComponent();
- }
- private void startButton_Click(object sender, EventArgs e)
- {
- Start();
- }
- private async void Start()
- {
- await Task.Delay(1000);
- sw = System.Diagnostics.Stopwatch.StartNew();
- InitializeCriticalSectionAndSpinCount(ref criticalSection, 0);
- calcThread = new Thread(() => CalcIntegral((new double[] { Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text), Convert.ToDouble(textBox3.Text) })));
- calcThread.Priority = ThreadPriority.Lowest;
- timeThread = new Thread(() => CalcTime());
- timeThread.Priority = ThreadPriority.Lowest;
- calcThread.Start();
- timeThread.Start();
- Thread.CurrentThread.Priority = ThreadPriority.Highest;
- }
- private void CalcTime()
- {
- while (n == 0){}
- while (true)
- {
- EnterCriticalSection(ref criticalSection);
- progress = counter / n;
- timeLeft = Math.Round((double)sw.ElapsedMilliseconds / 1000, 3);
- timeToFinish = Math.Round((timeLeft / progress - timeLeft), 3);
- LeaveCriticalSection(ref criticalSection);
- }
- }
- void CalcIntegral(double[] args)
- {
- var dataArray = args;
- double a = dataArray[0];
- double b = dataArray[1];
- n = Convert.ToInt32(dataArray[2]);
- long prevElapsed = sw.ElapsedMilliseconds;
- long deltaTime;
- double h;
- h = (b - a) / n;
- for (counter = 0; counter < n; counter++)
- {
- EnterCriticalSection(ref criticalSection);
- y = Math.Sin(x + 0.5 * h) + Math.Cos((x + 0.5 * h) * 0.9f);
- area += y * h;
- areaAbs += Math.Abs(y * h);
- deltaTime = sw.ElapsedTicks - prevElapsed;
- x += h;
- chartData.Points.AddXY(x, y);
- LeaveCriticalSection(ref criticalSection);
- }
- sw.Stop();
- }
- void RefreshUI()
- {
- SetControlPropertyThreadSafe(stepsText, "Text", counter.ToString());
- SetControlPropertyThreadSafe(xText, "Text", Math.Round(x, 5).ToString());
- SetControlPropertyThreadSafe(yText, "Text", Math.Round(y, 5).ToString());
- SetControlPropertyThreadSafe(progressBar1, "Value", (int)(100000 * progress));
- SetControlPropertyThreadSafe(timeToFinishText, "Text", timeToFinish.ToString());
- SetControlPropertyThreadSafe(timeText, "Text", timeLeft.ToString());
- }
- void StopButton_Click(object sender, EventArgs e)
- {
- calcThread.Abort();
- timeThread.Abort();
- sw.Stop();
- RefreshUI();
- chartData.ChartType = SeriesChartType.Line;
- chart.Series.Add(chartData);
- }
- public static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
- {
- if (control.InvokeRequired)
- {
- control.Invoke(new SetControlPropertyThreadSafeDelegate
- (SetControlPropertyThreadSafe),
- new object[] { control, propertyName, propertyValue });
- }
- else
- {
- control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[]
- {
- propertyValue
- });
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment