[英]How retrieve value of a Dispatcher.BeginInvoke in WPF/C#
It's my first question on this forum. 这是我在这个论坛上的第一个问题。 I'm a french student, so sorry for my English. 我是法国学生,对不起我的英语。 In my code i want to extract a value from a Dispatcher.BeginInvoke.
在我的代码中,我想从Dispatcher.BeginInvoke.
提取一个值Dispatcher.BeginInvoke.
Here is the part of my code that is problematic. 这是我的代码中有问题的部分。
public partial class Jouer : Window
{
// private Timer timer_creer_obstacle;
private Timer timer_personnage_chute;
private Timer timer_obstacles_avance;
private Personnage perso;
private Decor decor;
public Jouer()
{
InitializeComponent();
perso = new Personnage("canard-de-bain.png", World);
decor = new Decor(World);
// Timer qui gère l'avancé des obstacles
timer_obstacles_avance = new Timer();
timer_obstacles_avance.Enabled = true;
timer_obstacles_avance.Interval = 60;
timer_obstacles_avance.Elapsed += timer_obstacles_avance_event;
timer_obstacles_avance.AutoReset = true;
// Timer qui gère la chute du personnage
timer_personnage_chute = new Timer();
timer_personnage_chute.Enabled = true;
timer_personnage_chute.Interval = 60;
timer_personnage_chute.Elapsed += timer_personnage_chute_event;
timer_personnage_chute.AutoReset = true;
}
public void timer_personnage_chute_event(object sender, ElapsedEventArgs e)
{
perso.Dispatcher.BeginInvoke( // Dispatcher pour utiliser le multithearding
DispatcherPriority.Normal,
new Action(() =>
{
perso.Chuter();
perso.WorlFarmeCollision();
}));
And here is the code of WorldFarmeCollision
that return me a bool. 这是WorldFarmeCollision
的代码,该代码使我感到WorldFarmeCollision
。
public bool WorlFarmeCollision()
{
if (this.Y >= 350 || this.Y <= 0)
{
App.Current.Shutdown();
return true;
}
else
return false;
}
I want perso.WordFarmeCollision
return a value usable in my Jouer
class. 我想要perso.WordFarmeCollision
返回在Jouer
类中可用的值。
Thanks for your answers. 感谢您的回答。
BeginInvoke
is executed asynchronously, thus you can't get the return value instantly. BeginInvoke
是异步执行的,因此您不能立即获得返回值。
Change it into Invoke
instead. 改为将其更改为“ Invoke
。
public void timer_personnage_chute_event(object sender, ElapsedEventArgs e)
{
bool result = perso.Dispatcher.Invoke( // Dispatcher pour utiliser le multithearding
() =>
{
perso.Chuter();
return perso.WorlFarmeCollision();
}
, DispatcherPriority.Normal);
reference : https://msdn.microsoft.com/en-us/library/hh199426(v=vs.110).aspx 参考: https : //msdn.microsoft.com/zh-cn/library/hh199426(v=vs.110).aspx
I'm doing this by heart since i don't have a WPF environment set up at the moment. 由于我目前尚未设置WPF环境,因此我会全力以赴。 This should point you in the right direction though... 不过,这应该为您指明正确的方向...
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Perso perso = new Perso();
Func<bool> getWorlFarmeCollisionAsyncDelegate = perso.WorlFarmeCollision;
getWorlFarmeCollisionAsyncDelegate.BeginInvoke(
(resultASync) =>
{
var methodDelegate = (Func<bool>)resultASync.AsyncState;
perso.Dispatcher.BeginInvoke(
new Action(() => perso.BoolProperty = methodDelegate.EndInvoke(resultASync)));
},
getWorlFarmeCollisionAsyncDelegate);
}
}
public class Perso
{
public Dispatcher Dispatcher => Application.Current.Dispatcher;
public bool BoolProperty { get; set; }
public bool WorlFarmeCollision()
{
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.