Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to make a small application i am new to wpf what i am trying is i have seven buttons named as {button 1, button2 and so on} of different colors

theirs an method which generates random number {1,2,3,4} or {1,2} or someother combination

so if random numbers which are generated are {1,2} so button one should show animation and then after 2 seconds button 2 should should show animation

but the problem which i am facing is both the buttons are showing animation at same time

can anyone tell me how do i resolve it it will be very kind

this is the code

C#
public delegate void AddOperationDelegate();
public partial class MainWindow : Window
{
private void InvokeMessage(IAsyncResult ar)
{
AddOperationDelegate aod = (ar as AsyncResult).AsyncDelegate as AddOperationDelegate;
aod.EndInvoke(ar);

}
DispatcherTimer dispatcherTimer = new DispatcherTimer();
List<list><int>> fnlst = new List<list><int>>();
List<int> gmequestion = new List<int>();
List<int> gmeanswer = new List<int>();

List<list><int>> chklist = new List<list><int>>();
public MainWindow()
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0);
dispatcherTimer.Start();
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{

GenreateNumbers();

}
public void GenreateNumbers()
{

List<int> ldata = new List<int>();
Random r = new Random();
int va = r.Next(1, 7);
int a1 = va / 10;
int b = va % 10;
if (a1==0)
{
a1 = a1 + 1;
}
Random rnd = new Random();
var randomNumbers = Enumerable.Range(a1, b).OrderBy(i => rnd.Next()).ToArray();
randomNumbers = (from i in randomNumbers.ToArray()
orderby i ascending
select i).ToArray();
List<int> c = new List<int>();
foreach (var item in randomNumbers)
{
if (item==1)
{
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button1.BeginAnimation(Button.WidthProperty, a);
//FileInfo f = new FileInfo("..\\..\\Songs\\1.mp3");
//m1.Source = new Uri(f.FullName.ToString());
//m1.Play();

}
else if (item == 2)
{



DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button2.BeginAnimation(Button.WidthProperty, a);
//FileInfo f = new FileInfo("..\\..\\Songs\\2.wav");
//m1.Source = new Uri(f.FullName.ToString());
//m1.Play();

}
else if (item == 3)
{
Thread.Sleep(2000);
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button3.BeginAnimation(Button.WidthProperty, a);
//FileInfo f = new FileInfo("..\\..\\Songs\\3.wav");
//m1.Source = new Uri(f.FullName.ToString());
//m1.Play();

}
else if (item == 4)
{
Thread.Sleep(2000);
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button4.BeginAnimation(Button.WidthProperty, a);



}
else if (item == 5)
{
Thread.Sleep(2000);
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button5.BeginAnimation(Button.WidthProperty, a);
}
else if (item == 6)
{
Thread.Sleep(2000);
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button6.BeginAnimation(Button.WidthProperty, a);

}
else if (item == 7)
{
Thread.Sleep(2000);
DoubleAnimation a = new DoubleAnimation();
a.From = 50;
a.To = 100;
// Start animating
a.Duration = new Duration(TimeSpan.Parse("0:0:5"));
button7.BeginAnimation(Button.WidthProperty, a);
}
}
dispatcherTimer.Interval = new TimeSpan(0, 0, 15);



}

private void button1_Click(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\1.mp3");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();


}
private void button2_Click(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\2.wav");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\3.wav");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}


private void button4_Click_1(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\4.wav");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}

private void button5_Click_1(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\5.mp3");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}


private void button6_Click(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\6.mp3");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}


private void button7_Click_1(object sender, RoutedEventArgs e)
{
FileInfo f = new FileInfo("..\\..\\Songs\\7.mp3");
m1.Source = new Uri(f.FullName.ToString());
m1.Play();
}


public class CusComparer : IEqualityComparer<list><int>>
{
public bool Equals(List<int> x, List<int> y)
{
return x.SequenceEqual(y);
}

public int GetHashCode(List<int> obj)
{
int hashCode = 0;

for (var index = 0; index < obj.Count; index++)
{
hashCode ^= new { Index = index, Item = obj[index] }.GetHashCode();
}

return hashCode;
}
}

private void m7_MediaOpened(object sender, RoutedEventArgs e)
{

}

}
Posted
v2

1 solution

The problem is that you are probably using Sleep in the UI thread. You should never to this, as you don't delay animation, you simply delay the whole UI thread, making UI unresponsive. You should either use the animation embedded in WPF or use low-level animation with a separate thread (which can Sleep safely) and communication with UI thread using Dispatcher.

Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900