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:
Console.WriteLine(WordBank.LevelOneWords);
. Ok, you print some words on the console. This statement finishes normally and execution advances to the next statement.stopWatch.Start( );
. Ok, you start the stopwatch. Time starts ticking. Execution advances to the next statement.if ( stopWatch.Elapsed.Seconds >= timeInSeconds )
. Ok, this executes right after you started the stopwatch... a few nanoseconds after, maybe? IftimeInSeconds
is anything higher than 0,stopWatch.Elapsed.Seconds >= timeInSeconds
will be false.- 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
How to Determine If a Point Is Inside a 2D Convex Polygon
What Is the Most Recommended Way to Store Time in Postgresql Using Java
Spring Session-Scoped Beans (Controllers) and References to Services, in Terms of Serialization
How to Read a Text-File Resource into Java Unit Test
Making a Log4J Console Appender Use Different Colors for Different Threads
Copying Text to the Clipboard Using Java
What in the World Are Spring Beans
Java: Calling a Super Method Which Calls an Overridden Method
Must Issue a Starttls Command First
How to Get Java Logging Output to Appear on a Single Line
How to Prevent Eclipse from Hanging on Startup
How to Loop Over a Class Attributes in Java
How to Make Hashmap Work with Arrays as Key
Java:Does Wait() Release Lock from Synchronized Block
How to Solve the "A Generic Array of T Is Created for a Varargs Parameter" Compiler Warning
Qr Code Encoding and Decoding Using Zxing
How to Split a Text into Sentences Using the Stanford Parser
How to Deserialize a List Using Gson or Another JSON Library in Java