0

I have been trying to get a border in my wpf application(c#) to flash red and blue when you input the wrong password. my code:

//This method executes when you enter the wrong password
void Alert() 
{
    Count = 0;
    while (Count <= 5)
    {
        Border_.BorderBrush = Brushes.DarkRed;
        //Timer here
        Border_.BorderBrush = Brushes.DarkBlue;
        //Timer here
        Count++;
    }
}   

I have tried using await, Thread.Sleep(1000), Task.Delay(1000) and Timers but nothing worked. I searched for any similar question but could not find any solution that worked for me. (This question didnt work for me either: How to create a Flashing and Normal Background Visual States for a Border control)

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351

1 Answers1

-1

This is the code-behind example, without visual states;

public Task Alert()
{
    return Task.Run(() =>
    {
        int Count = 0;
        while (Count <= 5)
        {
            Application.Current.Dispatcher.Invoke(() => Border_.BorderBrush = Brushes.DarkRed);
            //Timer here
            Thread.Sleep(1000);
            Application.Current.Dispatcher.Invoke(() => Border_.BorderBrush = Brushes.DarkBlue);
            //Timer here
            Thread.Sleep(1000);
            Count++;
        }
    });
}

And you can call with;

await Alert();

Also, do not forget to set BorderThickness.

Akif T
  • 94
  • 8