What I want to do is a live background for my tic tac toe main menu which should put Xs and Os randomly in any button what happens is that the program just freeze and when I tried tracing it the code inside the dispatcher never get excuted
public partial class MainPage : PhoneApplicationPage
{
Random random;
private Button[] bts;
private int counter;
public MainPage()
{
InitializeComponent();
counter = 0;
bts = new[] { _1, _2, _3, _4, _5, _6, _7, _8, _9 };
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page3.xaml", UriKind.Relative));
}
private void About_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page4.xaml", UriKind.Relative));
}
private void form_Loaded(object sender, RoutedEventArgs e)
{
random = new Random();
while (true)
{
Dispatcher.BeginInvoke(()=>
{
bts[random.Next() % 9].Content = (counter % 2 == 0) ? "O" : "X";
counter++;
});
}
}
}
This is an infinite loop; it will freeze your program forever. (actually, until it runs out of memory in the dispatcher queue)
Don’t do that.