如何使用线程停止此三角动画 [英] how stop this trigonometric animation , using threads
本文介绍了如何使用线程停止此三角动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<Grid>
<Canvas Height="202" HorizontalAlignment="Left" Margin="10,97,0,0" Name="MyChart" VerticalAlignment="Top" Width="481" Background="#FFC3EEE3"></Canvas>
<Button Content="Iniciar" Height="23" Name="buIniciar" Width="75" Margin="10,12,418,276" Click="buIniciar_Click" />
<Button Content="Detener" Height="23" Name="buDetener" Width="75" Margin="10,45,418,243" Click="buDetener_Click" />
</Grid>
public partial class MainWindow : Window
{
double time = 0;
Polyline pl;
TranslateTransform translate;
ManualResetEvent evtObj = new ManualResetEvent(false);
public MainWindow()
{
InitializeComponent();
}
public float ValorFuncion1(double X)
{
return Convert.ToSingle(Math.Sin(X));
}
public float ValorFuncion2(double X)
{
return Convert.ToSingle(Math.Abs(Math.Cos(X)) * Math.Sin(X) + Math.Abs(Math.Sin(X)) * Math.Cos(X));
}
public void DoAnimate(int i)
{
//while (true)
//{
if (MyChart.Dispatcher.Thread == Thread.CurrentThread)
{
pl = new Polyline() { Stroke = new SolidColorBrush(Colors.Red), StrokeThickness = 1.5f };
translate = new TranslateTransform(0.0, (MyChart.Height / 2.0f));
pl.RenderTransform = translate;
MyChart.Children.Add(pl);
CompositionTarget.Rendering += delegate(object s, EventArgs args)
{
Double PosX = pl.ActualWidth;
if (PosX >= MyChart.Width)
{
pl.Points.RemoveAt(0);
translate.X--;
}
double x = time * 0.05;
double y = Math.Cos(x);
pl.Points.Add(new Point(time, y * 30));
time++;
};
}
else
{
MyChart.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => DoAnimate(i)));
}
// evtObj.WaitOne(1000);
//}
}
private void buIniciar_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 1; i++)
{
int j = i;
ThreadStart ts = delegate() { DoAnimate(j); };
Thread t = new Thread(ts);
t.Start();
}
}
private void buDetener_Click(object sender, RoutedEventArgs e)
{
evtObj.Set();
}
}
}
伙计们,不知道我在做什么错,帮我先谢谢你.
Please guys, do not know what I''m doing wrong, help me I thank you in advance
推荐答案
我的解决方法如下:
为委托创建函数,而不要创建任何函数
My soulution is as follows:
Create a function for the delegate instead of anynymous
private void doDelegate(object s, EventArgs args)
{
Double PosX = pl.ActualWidth;
if (PosX >= MyChart.Width)
{
pl.Points.RemoveAt(0);
translate.X--;
}
double x = time * 0.05;
double y = Math.Cos(x);
pl.Points.Add(new Point(time, y * 30));
time++;
}
然后将其分配给线程
Then assign it to the thread
CompositionTarget.Rendering += doDelegate;
然后更换定位器
Then change the detener
private void buDetener_Click(object sender, RoutedEventArgs e)
{
CompositionTarget.Rendering -= doDelegate;
}
这篇关于如何使用线程停止此三角动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文