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"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<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"/>
</Grid>
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace WpfTimer
{
public partial class MainWindow : Window
{
BackgroundWorker parlante;
public MainWindow()
{
InitializeComponent();
parlante = new BackgroundWorker();
parlante.DoWork += sonar;
parlante.RunWorkerCompleted += apagar;
}
private void sonar(object sender, DoWorkEventArgs e)
{
//Reproducir sonido
//demorar hasta el siguiente beep
System.Threading.Thread.Sleep(5000);
}
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;
parlante.RunWorkerAsync();
}
}
}
}