Browse Source

Первичная реализация запуска и паузирования отрисовки процедуры при помощи класса DrawTask

master
Никита 6 years ago
parent
commit
42d419a24f
  1. 90
      Signal Generator/DrawTask.cs
  2. 1
      Signal Generator/Signal Generator.csproj
  3. 6
      Signal Generator/TaskWindow.xaml
  4. 56
      Signal Generator/TaskWindow.xaml.cs

90
Signal Generator/DrawTask.cs

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Signal_Generator
{
public class DrawTask : Task
{
Canvas currentCanvas;
System.Timers.Timer taskTimer;
Stopwatch stopwatch = new Stopwatch();
MultiSignal currentMultiSignal;
public DStatus dStatus;
public enum DStatus
{
Created, Running, Paused, Finished, Canceled
}
public DrawTask(Action a) : base(a) { }
public void buildTask(MultiSignal s, Canvas c)
{
currentCanvas = c;
currentMultiSignal = s;
dStatus = DStatus.Running;
drawSignalAtCanvas();
stopwatch.Start();
taskTimer = new System.Timers.Timer(2000);
taskTimer.Elapsed += updateTextForms;
taskTimer.Enabled = true;
}
private void updateTextForms(object sender, ElapsedEventArgs e)
{
var elapsed = stopwatch.ElapsedMilliseconds;
}
private void drawSignalAtCanvas()
{
double lastX = 0, lastY = 0;
double w = currentCanvas.ActualWidth;
for (int i = 0; i != (int)w; i++)
{
double y = currentMultiSignal.currentAmplitude(i / 5.0);
drawLine(lastX, lastY, i, y);
lastX = i;
lastY = y;
Thread.Sleep(10);
switch (dStatus)
{
case DStatus.Paused:
while (dStatus == DStatus.Paused)
Thread.Sleep(10);
break;
case DStatus.Canceled: return;
}
}
dStatus = DStatus.Finished;
}
private void drawLine(double x1, double y1, double x2, double y2)
{
Action a = () => {
Line l = new Line();
l.StrokeThickness = 1;
l.Stroke = Brushes.Red;
l.X1 = x1;
l.Y1 = remap(y1);
l.X2 = x2;
l.Y2 = remap(y2);
currentCanvas.Children.Add(l);
};
currentCanvas.Dispatcher.Invoke(a);
}
private double remap(double x, double oldMin = -100.0, double oldMax = 100.0, double newMin = 100.0, double newMax = 0.0)
{
newMin = currentCanvas.ActualHeight;
return newMin + (x - oldMin) * (newMax - newMin) / (oldMax - oldMin);
}
}
}

1
Signal Generator/Signal Generator.csproj

@ -58,6 +58,7 @@
<Compile Include="AddSignalWindow.xaml.cs"> <Compile Include="AddSignalWindow.xaml.cs">
<DependentUpon>AddSignalWindow.xaml</DependentUpon> <DependentUpon>AddSignalWindow.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="DrawTask.cs" />
<Compile Include="Signal\Base\AnalogSignalMethods.cs" /> <Compile Include="Signal\Base\AnalogSignalMethods.cs" />
<Compile Include="Signal\FmSignal.cs" /> <Compile Include="Signal\FmSignal.cs" />
<Compile Include="Settings.cs" /> <Compile Include="Settings.cs" />

6
Signal Generator/TaskWindow.xaml

@ -26,7 +26,9 @@
<TextBox x:Name="currentTime_TextBox" Margin="0,0,0,5" Grid.Row="1" Grid.Column="2"/> <TextBox x:Name="currentTime_TextBox" Margin="0,0,0,5" Grid.Row="1" Grid.Column="2"/>
<TextBlock x:Name="currentAmplitude_TextBlock" Margin="0,0,5,5" Grid.Row="2" Grid.Column="1">Текущий сигнал:</TextBlock> <TextBlock x:Name="currentAmplitude_TextBlock" Margin="0,0,5,5" Grid.Row="2" Grid.Column="1">Текущий сигнал:</TextBlock>
<TextBox x:Name="currentAmplitude_TextBox" Margin="0,0,0,5" Grid.Row="2" Grid.Column="2"/> <TextBox x:Name="currentAmplitude_TextBox" Margin="0,0,0,5" Grid.Row="2" Grid.Column="2"/>
<Button x:Name="start_Button" Width="60" Content="Старт" Margin="5,0" Grid.Row="3" Grid.Column="1" Click="start_Button_Click"/> <Button x:Name="start_Button" Width="120" Content="Запустить" Margin="5,0" Grid.Row="3" Grid.Column="0"
<Button x:Name="pause_Button" Width="60" Content="Пауза" Margin="5,0" Grid.Row="3" Grid.Column="2"/> Grid.ColumnSpan="2" Click="start_Button_Click"/>
<Button x:Name="pause_Button" Width="120" Content="Приостановить" Margin="5,0" Grid.Row="3" Grid.Column="2"
Grid.ColumnSpan="2" Click="pause_Button_Click"/>
</Grid> </Grid>
</Window> </Window>

56
Signal Generator/TaskWindow.xaml.cs

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
@ -17,49 +18,52 @@ namespace Signal_Generator
/// <summary> /// <summary>
/// Логика взаимодействия для TaskWindow.xaml /// Логика взаимодействия для TaskWindow.xaml
/// </summary> /// </summary>
///
public partial class TaskWindow : Window public partial class TaskWindow : Window
{ {
MultiSignal currentMultiSignal; MultiSignal currentMultiSignal;
DrawTask currentTask;
public TaskWindow(MultiSignal s) public TaskWindow(MultiSignal s)
{ {
InitializeComponent(); InitializeComponent();
currentMultiSignal = s; currentMultiSignal = s;
} }
public void drawSignal(MultiSignal s) private void start_Button_Click(object sender, RoutedEventArgs e)
{ {
double lastX = 0, lastY = 0; signalGraph_Canvas.Children.Clear();
double w = signalGraph_Canvas.ActualWidth; if (currentTask != null)
for (int i = 0; i != (int)w; i++) switch (currentTask.dStatus)
{ {
double y = s.currentAmplitude(i / 5.0); case DrawTask.DStatus.Paused:
drawLine(lastX, lastY, i, y); goto case DrawTask.DStatus.Running;
lastX = i; case DrawTask.DStatus.Running:
lastY = y; currentTask.dStatus = DrawTask.DStatus.Canceled;
} break;
} }
Action newAction = () =>
public void drawLine (double x1, double y1, double x2, double y2)
{ {
Line l = new Line(); currentTask.buildTask(currentMultiSignal, signalGraph_Canvas);
l.StrokeThickness = 1; };
l.Stroke = Brushes.Red; currentTask = new DrawTask(newAction);
l.X1 = x1; currentTask.Start();
l.Y1 = remap(y1);
l.X2 = x2;
l.Y2 = remap(y2);
signalGraph_Canvas.Children.Add(l);
} }
public double remap (double x, double oldMin = -100.0, double oldMax = 100.0, double newMin = 100.0, double newMax = 0.0) private void pause_Button_Click(object sender, RoutedEventArgs e)
{ {
newMin = signalGraph_Canvas.ActualHeight; switch (currentTask.dStatus) {
return newMin + (x - oldMin) * (newMax - newMin) / (oldMax - oldMin); case DrawTask.DStatus.Paused:
currentTask.dStatus = DrawTask.DStatus.Running;
pause_Button.Content = "Приостановить";
break;
case DrawTask.DStatus.Running:
currentTask.dStatus = DrawTask.DStatus.Paused;
pause_Button.Content = "Возобновить";
break;
} }
private void start_Button_Click(object sender, RoutedEventArgs e)
{
drawSignal(currentMultiSignal);
} }
} }
} }

Loading…
Cancel
Save