How can I make a sound sound every so often?


I am trying to realize that when a hand passes a coordinate, a sound will sound. The problem arises when passing the hand, which is tried to run constantly since it is a sound of about 2 seconds. Could you do it in a way that you do it every so often?

//Sector 3
if (FuncionActivada == 1 && VariablesGlobales.HandRightX >= 0.38 && VariablesGlobales.HandRightY >= 0.35 && VariablesGlobales.HandRightZ >= 0.9
    && VariablesGlobales.HandRightZ <= 1.3)
    SoundPlayer miSonido3 = new SoundPlayer(@"c:\Windows\Media\Ring03.wav");
asked by Daniel Potrimba 30.05.2017 в 11:03

2 answers


You could put a timer :

DispatcherTimer dispathcer  = new DispatcherTimer();

//Intervalo de 1 segundo
dispatcher.Interval = new TimeSpan (0,0,1);
dispatcher.Tick += (s, a) => {
//la función a ejecutar, en este caso el sonido
answered by 30.05.2017 в 11:07

Implement a small solution that when you pass the mouse over a rectangle it shows in a label "beep .." and after finishing the sound it waits for five seconds and goes off.

I added that it shows the ticks so that you see that no matter how many times you enter and leave the rectangle it does not sound again until the previous one ends.

I use BackgroundWorker to control the flow of execution in a thread.

<Window x:Class="WpfTimer.MainWindow"
    Title="MainWindow" Height="350" Width="525">
    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="100" MouseEnter="Rectangle_MouseEnter"/>
    <Label Name="lblSalida" Content="Label" HorizontalAlignment="Left" Margin="115,10,0,0" VerticalAlignment="Top" Width="392"/>

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace WpfTimer
public partial class MainWindow : Window
    BackgroundWorker parlante;

    public MainWindow()
        parlante = new BackgroundWorker();
        parlante.DoWork += sonar;
        parlante.RunWorkerCompleted += apagar;

    private void sonar(object sender, DoWorkEventArgs e)
        //Reproducir sonido
        //demorar hasta el siguiente beep

    private void apagar(object sender, RunWorkerCompletedEventArgs e)
        lblSalida.Content = "";

    private void Rectangle_MouseEnter(object sender, MouseEventArgs e)
        if (!parlante.IsBusy)
            lblSalida.Content = "beep.. " + DateTime.Now.Ticks;
answered by 30.05.2017 в 15:52