C#系统。服务器上带有倒计时的计时器

C#系统。服务器上带有倒计时的计时器,c#,xaml,timer,visual-studio-2013,C#,Xaml,Timer,Visual Studio 2013,我希望你能帮我一点忙 因此,主要的问题是:如何创建一个服务器端计时器,它有60秒的倒计时,间隔为1秒,如果时间为零,它应该执行一个操作?f、 e.跳过球员的回合 它基于具有多客户端和一台服务器的纸牌游戏。在客户端,我确实有一个计时器,但我想这没有任何意义,因为唯一能看到计时器的人是玩家自己,所以我确实需要服务器上的逻辑向所有其他客户端显示时间 这是到目前为止我的代码。但到目前为止还没有行动 ... using System.Timers; namespace CardGameServer{

我希望你能帮我一点忙

因此,主要的问题是:如何创建一个服务器端计时器,它有60秒的倒计时,间隔为1秒,如果时间为零,它应该执行一个操作?f、 e.跳过球员的回合

它基于具有多客户端和一台服务器的纸牌游戏。在客户端,我确实有一个计时器,但我想这没有任何意义,因为唯一能看到计时器的人是玩家自己,所以我确实需要服务器上的逻辑向所有其他客户端显示时间

这是到目前为止我的代码。但到目前为止还没有行动

...
using System.Timers;

namespace CardGameServer{

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,
                 ConcurrencyMode = ConcurrencyMode.Reentrant)]
public sealed class Player : IPlayer
{
    public Timer timer;
    int remainingtime = 60;

 public void timerAnzeigen()
    {
        timer = new Timer(1000);
        timer.Elapsed += new ElapsedEventHandler(OnTimeEvent);
        timer.AutoReset = true;
        timer.Enabled = true;
        timer.Start();
    }

    private void OnTimeEvent(object source, ElapsedEventArgs e)
    {
        remainingtime--;
        if (remainingtime == 0)
        {
            drawCard();
            nextPlayer();
            remainingtime = 60;
        }
    }

我注意到您的标签中有Visual Studio 2013,所以我假设您使用的是C#4.5。在这种情况下,我建议不要使用
Timer
类,而是使用
Task
async/await

例如:

using System.Threading;
using System.Threading.Tasks;

public sealed class TaskTimer
{
    Action onTick;
    CancellationTokenSource cts;

    public bool IsRunning { get; private set; }

    public TimeSpan Interval { get; set; }

    public TaskTimer(TimeSpan interval, Action onTick)
    {
        this.Interval = interval;
        this.onTick = onTick;
    }

    public async void Start()
    {
        Stop();

        cts = new CancellationTokenSource();
        this.IsRunning = true;

        while (this.IsRunning)
        {
            await Task.Delay(this.Interval, cts.Token);

            if (cts.IsCancellationRequested)
            {
                this.IsRunning = false;
                break;
            }

            if (onTick != null)
                onTick();
        }
    }

    public void Stop()
    {
        if (cts != null)
            cts.Cancel();
    }
}
然后在您的
Player
课程中:

sealed class Player : IPlayer
{
    static readonly TimeSpan OneSecondDelay = TimeSpan.FromSeconds(1);
    static readonly int InitialSeconds = 60;

    TaskTimer timer;
    long remainingSeconds;

    public int RemainingSeconds
    {
        get { return (int)Interlocked.Read(ref remainingSeconds); }
    }

    public Player()
    {
        ResetTimer(InitialSeconds);

        timer = new TaskTimer(OneSecondDelay, TimerTick);
        timer.Start();
    }

    void ResetTimer(int seconds)
    {
        Interlocked.Exchange(ref remainingSeconds, seconds);
    }

    void TimerTick()
    {
        var newValue = Interlocked.Decrement(ref remainingSeconds);

        if (newValue <= 0)
        {
            // Timer has expired!
            ResetTimer(InitialSeconds);
        }
    }
}
密封类玩家:IPlayer
{
静态只读TimeSpan OneSecondDelay=TimeSpan.FromSeconds(1);
静态只读int InitialSeconds=60;
任务计时器;
长余秒;
公共整数剩余秒数
{
获取{return(int)interlocated.Read(ref remainingSeconds);}
}
公共玩家()
{
重置计时器(初始化秒);
计时器=新任务计时器(OneSecondDelay,TimerCheck);
timer.Start();
}
无效重置计时器(整数秒)
{
联锁交换(参考剩余秒,秒);
}
void TimerTick()
{
var newValue=联锁递减(参考剩余秒);

如果(newValue为什么不简单地将剩余时间整数发送到服务器,并用一个标志指示它是什么,然后让服务器简单地将其转发给所有客户端?我同意@StevenMills的另一个选择是,如果这是一个回合制游戏,则让他们在相同的相对时间启动计时器,然后在其回合结束后的客户端有一个等待屏幕s定时器触发它的事件告诉所有其他客户端该事件已经发生。一旦所有客户端返回,他们将收到该事件,开始下一轮。我明白你的意思,但我没有想到,thx!