Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have the following Visual Studio code: public partial class MainPage : PhoneAp

ID: 643179 • Letter: I

Question

I have the following Visual Studio code:

public partial class MainPage : PhoneApplicationPage
{
Random random;
int aWidth;
int aHeight;
int score;
DispatcherTimer timer;

// Constructor
public MainPage()
{
InitializeComponent();
random = new Random();
timer = new DispatcherTimer();
timer.Start();
timer.Interval = new TimeSpan(0, 0, 0, 1, 0);
timer.Tick += new EventHandler(timer_Tick);
  

  
}

void timer_Tick(object sender, EventArgs e)
{
int x = random.Next(aWidth) - (int)button1.ActualWidth;
int y = random.Next(aHeight) - (int)button1.ActualHeight;
if (x < 10)
x = 10;
if (y < 10)
y = 10;
  
}
  
private void ContentPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
aWidth = (int)e.NewSize.Width;
aHeight = (int)e.NewSize.Height;
}

private void button1_Click(object sender, RoutedEventArgs e)
{
score += 1;
ScoreTextBlock.Text = "Score: " + score;
}
}
}

How can I make it stop after 30 seconds?

Explanation / Answer

There are two ways to set the timer to stop. They are:

One way is to set the time.Interval option as

timer.Interval=TimeSpan.FromSeconds(30);

Other way is just write a function

               private void turnoff() //now your this method will work to stop the timer

               {

                               timer.stop();

               }

But to use appropriately, need to set the timer.Interval part either as

                   timer.Interval=TimeSpan.FromSecond(30);

or

                   timer.Interval = 30000;

And rewrite the statements as follows:

timer.Tick += new EventHandler(timer_Tick);

timer.Interval=TimeSpan.FromSeconds(30);