Stop a Stopwatch

Should I Stop Stopwatch at the end of the method?

No, you don't need to stop it. Stop() just stops tracking elapsed time. It does not free up any resources.

How do I stop a stopwatch at a certain amount of time?

Ok, let's go through what your code does:

  1. Console.WriteLine(WordBank.LevelOneWords);. Ok, you print some words on the console. This statement finishes normally and execution advances to the next statement.
  2. stopWatch.Start( );. Ok, you start the stopwatch. Time starts ticking. Execution advances to the next statement.
  3. if ( stopWatch.Elapsed.Seconds >= timeInSeconds ). Ok, this executes right after you started the stopwatch... a few nanoseconds after, maybe? If timeInSeconds is anything higher than 0, stopWatch.Elapsed.Seconds >= timeInSeconds will be false.
  4. Execution goes on...

How and when did you expect that seconds would go by between steps 2 and 3?

So, how do you fix this? Well the easy way, is to let the user type the words, no matter how slow he might be: userType = Console.ReadLine( );. Only then check the elapsed time and if its greater than timeInSeconds inform the user he was too slow.

Flutter start and stop stopwatch from parent widget

This line of code can't stop the stop watch because it is stopping a new instance of StopWatch which is different from the StopWatch you have used.

StopWatch().stop();

Use a boolean to control the stop and start of the stopWatch

MyWidget change to stateful widget

 bool isStop = false;

@override
Widget build(BuildContext context) {
return Column(children: [
StopWatch(isStop: isStop),
RaisedButton(
onPressed: () {
setState(() {
isStop = true;
});
},
child: Text('stop')),
RaisedButton(
onPressed: () {
setState(() {
isStop = false;
});
},
child: Text('start'))
]);
}

StopWatch:

class StopWatch extends StatefulWidget {
final bool isStop;

const StopWatch({Key key, this.isStop}) : super(key: key);

@override
_StopWatchState createState() => _StopWatchState();
}

class _StopWatchState extends State<StopWatch> {
//...some code

@override
void didUpdateWidget(StopWatch oldWidget) {
super.didUpdateWidget(oldWidget);
// Check isStop value is different from the previous state
// this function will trigger when every time this widget is build
if (oldWidget.isStop != widget.isStop) {
widget.isStop ? stop() : start();
}
}

Not suggest to call the Widget function in the parent Widget, since every time build, there will be a new instance of the Widget

how to stop a stopwatch with a button click in Xamarin?

Use the StopWatch as a member Variable and stop the watch in an OnStopClicked-Event. Read the elapsed time after stopping. Instead of button.IsVisible you could also use button.IsEnabled.

public partial class MainPage : ContentPage
{
Stopwatch mStopWatch = new Stopwatch();

public MainPage()
{
InitializeComponent();
}

private void StartButton_Clicked(object sender, EventArgs e)
{
startButton.IsVisible = false;
BG.BackgroundColor = Color.Red;
status_text.Text = "Get Ready";
Random R = new Random();

Device.StartTimer(TimeSpan.FromSeconds(R.Next(3, 10)), () =>
{
mStopWatch.Start();
stopButton.IsVisible = true;
BG.BackgroundColor = Color.Green;
return false;
});
}

private void StopButton_Clicked(object sender, EventArgs e)
{
mStopWatch.Stop();
long elapsed = mStopWatch.ElapsedMilliseconds;
status_text.Text = elapsed.ToString();
mStopWatch.Reset();
stopButton.IsVisible = false;
startButton.IsVisible = true;
}
}


Related Topics



Leave a reply



Submit