C# 如何等待计时器停止?
我正在尝试使用C#构建一个21点游戏。 我有一个赌场类和球员,卡和甲板类是赌场的对象。 赌场向具有以下功能的玩家随机发放一张卡:C# 如何等待计时器停止?,c#,winforms,timer,wait,C#,Winforms,Timer,Wait,我正在尝试使用C#构建一个21点游戏。 我有一个赌场类和球员,卡和甲板类是赌场的对象。 赌场向具有以下功能的玩家随机发放一张卡: public void giveRandomCardTo(Player P) { P.takeCard(this.deck.getRandomCard()); } public void giveRandomCardTo(Player P) { while
public void giveRandomCardTo(Player P)
{
P.takeCard(this.deck.getRandomCard());
}
public void giveRandomCardTo(Player P)
{
while (_timerRunning) {/*wait*/ }
this.currentDealingID = P.id;
if (this.currentDealingID >= 0 && this.currentDealingID < this.NumberOfPlayers && this.currentDealingID!=10)
{//Checking the current player is not the dealer.
this.MovingCard.Show(); //Moving card is a picture box with a closed card
_timerRunning=true;
T.Start();
}
P.takeCard(this.deck.getRandomCard());
}
这工作得很好,但是我想添加一个动画,比如使用计时器将一张闭合的卡片图像移动到玩家的卡片图片盒中。因此,我将此部分添加到函数中:
public void giveRandomCardTo(Player P)
{
P.takeCard(this.deck.getRandomCard());
}
public void giveRandomCardTo(Player P)
{
while (_timerRunning) {/*wait*/ }
this.currentDealingID = P.id;
if (this.currentDealingID >= 0 && this.currentDealingID < this.NumberOfPlayers && this.currentDealingID!=10)
{//Checking the current player is not the dealer.
this.MovingCard.Show(); //Moving card is a picture box with a closed card
_timerRunning=true;
T.Start();
}
P.takeCard(this.deck.getRandomCard());
}
public-void-cardto(玩家P)
{
当(_timerRunning){/*等待*/}
this.currentDealingID=P.id;
如果(this.currentDealingID>=0&&this.currentDealingID
定时器T的Timer.Tick eventhandler是:
public void TimerTick(object sender, EventArgs e)
{
movingcardvelocity = getVelocityFromTo(MovingCard.Location, this.Players[this.currentDealingID].CardPBS[0].Location);
double divide=5;
movingcardvelocity = new Point((int)(movingcardvelocity.X / divide), (int)(movingcardvelocity.Y / divide));
this.MovingCard.Location = new Point(this.MovingCard.Location.X + movingcardvelocity.X, this.MovingCard.Location.Y + movingcardvelocity.Y);
//Stop if arrived:
double epsilon = 20;
if (Distance(this.MovingCard.Location, this.Players[this.currentDealingID].CardPBS[0].Location) < epsilon)
{
_timerRunning=false;
this.MovingCard.Hide();
T.Stop();
}
}
public void TimerTick(对象发送方,事件参数e)
{
movingcardvelocity=getVelocityFromTo(MovingCard.Location,this.Players[this.currentDealingID].CardPBS[0].Location);
双除法=5;
movingcardvelocity=新点((int)(movingcardvelocity.X/divide),(int)(movingcardvelocity.Y/divide));
this.MovingCard.Location=新点(this.MovingCard.Location.X+movingcardvelocity.X,this.MovingCard.Location.Y+movingcardvelocity.Y);
//到达时停止:
双ε=20;
if(距离(this.MovingCard.Location,this.Players[this.currentDealingID].CardPBS[0].Location)
定时器也很好地工作。但当我一张接一张地玩纸牌时,我必须等到第一个动画完成。而void to
中的while(_timerRunning){/*wait*/}
行将程序阻塞在无限循环中
我怎样才能让它等到bool\u timerRunning=false
谢谢您的帮助。它可以对您有所帮助-方法
有一个示例,您可以重用/修改,无需等待。只需从
勾选事件处理程序调用您的方法,而无需使用\u timerRunning
标志。停止计时器并将卡交给玩家:
T.Stop();
this.MovingCard.Hide();
giveRandomCardTo(this.Players[this.currentDealingID]);
我还创建了一个方法iscardarrievedto(点位置)
,以简化条件逻辑:
public void TimerTick(object sender, EventArgs e)
{
var player = Players[currentDealingID];
Point pbsLocation = player.CardPBS[0].Location;
MoveCard();
if (IsCardArrivedTo(pbsLocation))
{
MovingCard.Hide();
T.Stop();
giveRandomCardTo(player);
}
}
private bool IsCardArrivedTo(Point location)
{
double epsilon = 20;
return Distance(MovingCard.Location, location) < epsilon;
}
private void MoveCard()
{
// calculate new location
MovingCard.Location = newLocation;
}
public void TimerTick(对象发送方,事件参数e)
{
var player=Players[currentDealingID];
点pbsLocation=player.CardPBS[0]。位置;
MoveCard();
如果(已发送到(PBS位置))
{
MovingCard.Hide();
T.停止();
给(玩家)一张卡片;
}
}
专用布尔值已到达(点位置)
{
双ε=20;
返回距离(移动卡位置、位置)
顺便说一句,在C#中,我们使用CamelCasing作为方法名。谢谢您的回答。不过,我从未理解线程:)WaitOne-在当前WaitHandle接收到信号之前阻止当前线程。
除了冻结ui之外,这将有什么帮助?只有在单独的线程中处理实际工作时才有帮助这起作用。我有很多像这样的1或2行函数,事实上我开始避免这些,特别是如果我曾经使用过它们。感谢您的回答:)@marvin最好使用带有描述性名称的小方法,而不是处理大量代码并编写注释,如//如果到达请停止:
:)ok:)但此解决方案仍然存在问题,我修改了代码,以便玩家在计时器中接收卡。Stop()因此giveRandomCardTo()已成为AnimateCard(){},但我仍有for(I=0:i@marvin只需为每个玩家启动计时器-当计时器停止时,它将调用animatecard(player[i]);
ok,我定义了一个队列处理,在计时器停止时,它将下一个玩家和animatecard(next)排队。感谢您的帮助:)