Java Swing Timer

This simple program works for me: import java.awt.event.*; import javax.swing.*; public class Test { public static void main(String [] args) throws Exception{ ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { //…Perform a task… System.out.println(“Reading SMTP Info.”); } }; Timer timer = new Timer(100 ,taskPerformer); timer.setRepeats(false); timer.start(); Thread.sleep(5000); } }

java.util.Timer: Is it deprecated?

As others have mentioned, no it is not deprecated but I personally always use ScheduledExecutorService instead as it offers a richer API and more flexibility: ScheduledExecutorService allows you to specify the number of threads whereas Timer always uses a single thread. ScheduledExecutorService can be constructed with a ThreadFactory allowing control over thread aspects other than … Read more

Assembly CPU frequency measuring algorithm

Intel CPUs after Core Duo support two Model-Specific registers called IA32_MPERF and IA32_APERF. MPERF counts at the maximum frequency the CPU supports, while APERF counts at the actual current frequency. The actual frequency is given by: You can read them with this flow ; read MPERF mov ecx, 0xe7 rdmsr mov mperf_var_lo, eax mov mperf_var_hi, … Read more

How to detect when button pressed and released on android

Use OnTouchListener instead of OnClickListener: // this goes somewhere in your class: long lastDown; long lastDuration; … // this goes wherever you setup your button listener: button.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { lastDown = System.currentTimeMillis(); } else if (event.getAction() == MotionEvent.ACTION_UP) { lastDuration = System.currentTimeMillis() – … Read more

How to get duration, as int milli’s and float seconds from ?

Is this what you’re looking for? #include <chrono> #include <iostream> int main() { typedef std::chrono::high_resolution_clock Time; typedef std::chrono::milliseconds ms; typedef std::chrono::duration<float> fsec; auto t0 = Time::now(); auto t1 = Time::now(); fsec fs = t1 – t0; ms d = std::chrono::duration_cast<ms>(fs); std::cout << fs.count() << “s\n”; std::cout << d.count() << “ms\n”; } which for me prints … Read more

How to apply a fade transition effect to Images using a Timer?

There are some problem to fix here: → fadeTimer.Interval = 10;: You’re (possibly) using the wrong Timer: the System.Timers.Timer‘s Elapsed is raised in a ThreadPool Thread. Unless you have set the SynchronizingObject to a Control object, which is then used to marshal the handler calls, referencing a Control in the handler will cause trouble (cross-thread … Read more

What happens to timer in standby mode? [closed]

Ok. I asked my friend and this are his results: 23:21:32 : Timer started 23:21:35 : PC Goes Sleep 23:22:50 : PC Wakes 23:22:50 : Timer fired 23:23:50 : Timer fired using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace Test { class Program { static System.Timers.Timer timer; static void Main(string[] args) { timer = … Read more

how to use a swing timer to start/stop animation

import javax.swing.Timer; Add an attribute; Timer timer; boolean b; // for starting and stoping animation Add the following code to frame’s constructor. timer = new Timer(100, new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { // change polygon data // … repaint(); } }); Override paint(Graphics g) and draw polygon from the data that was … Read more