0

I am making a turn based silverlight game(Cards game). and I want to delay between turns.

I've tried the Thread.Sleep, but it halts my UI. I've tried to use DispatcherTimer, but it acts funny. sometimes it works, sometimes it skips.

My code works perfectly with DipatcherTimer when i set the Interval to 3 seconds, but when i set the interval to 1 second, it starts to skip some rounds.

Is there another way to create this delay?

Update: I just restarted my windows, and it worked perfectly for a while. One hour later, I tried again, without changing the code, it started skipping! i dont get it.

Ateik
  • 2,458
  • 4
  • 39
  • 59
  • There are other timers available - http://msdn.microsoft.com/en-us/library/system.threading.timer(v=VS.95).aspx - for example – ChrisF Nov 11 '11 at 13:17
  • tried that, still got the same issue. – Ateik Nov 11 '11 at 13:40
  • Any chance you can use an animation storyboard? As you might as well show something during the delay you could make that one second visually appealing and catch the animation end event. – iCollect.it Ltd Nov 11 '11 at 13:50
  • Sounds to me that dispatcher timer is the tool for the job. The problem is more likely to be in how you are using. Show us some relevant code. – AnthonyWJones Nov 11 '11 at 13:54
  • the sotryboard idea sounds good, ill try it – Ateik Nov 11 '11 at 14:20
  • and ill try to post some code, but i dont think thats the problem. my code works perfectly when i use 3 seconds interval. – Ateik Nov 11 '11 at 14:21

1 Answers1

1

You can use the System.Threading.Timer class with the understanding that it uses threads (as seen below). The timer is setup in the constructor. It starts immediately (the third parameter set to 0), and then executes every 1000ms (4th param). Inside, the code immediately calls the Dispatcher to update the UI. The potential benefit of this is that you're not tying up the UI thread for busy work that could be done in another thread (without using a BackgroundWorker for example).

using System.Windows.Controls;
using System.Threading;

namespace SLTimers
{
    public partial class MainPage : UserControl
    {
        private Timer _tmr;
        private int _counter;
        public MainPage()
        {
            InitializeComponent();
            _tmr = new Timer((state) =>
            {
                ++_counter;
                this.Dispatcher.BeginInvoke(() =>
                {
                    txtCounter.Text = _counter.ToString();
                });
            }, null, 0, 1000);            
        }
    }
}

<UserControl x:Class="SLTimers.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name="txtCounter"  Margin="12" FontSize="80" Text="0"/>
    </Grid>
</UserControl>
WiredPrairie
  • 58,954
  • 17
  • 116
  • 143