thread-sleep

Python threading interrupt sleep

偶尔善良 提交于 2019-12-18 09:00:43
问题 Is there a way in python to interrupt a thread when it's sleeping? (As we can do in java) I am looking for something like that. import threading from time import sleep def f(): print('started') try: sleep(100) print('finished') except SleepInterruptedException: print('interrupted') t = threading.Thread(target=f) t.start() if input() == 'stop': t.interrupt() The thread is sleeping for 100 seconds and if I type 'stop', it interrupts 回答1: How about using condition objects: https://docs.python

How to add a delay to a WPF program without blocking the UI

六眼飞鱼酱① 提交于 2019-12-18 07:07:34
问题 I am building a device emulator. When it starts, it takes some time for it to initialized. This would be logically represented by being turned on and going immediately to an "Initialization" state, and after some time it goes to "Ready" state. I am using MVVM, so the ViewModel will for now represent all the device logic. Each of the possible states have a datatriggered style to be rendered by the View. If I just set the state when I build the viewmodel, the view renders with the correct

Android Sleep/Wait/Delay function

时光总嘲笑我的痴心妄想 提交于 2019-12-18 03:14:06
问题 first of all, I'm a beginner to android world so please apologize me if it is stupid question.. I'm trying to do following: Enable Mobile Data Wait for 10 seconds a. check if Mobile got IP address (data connected sucessfully) b. if Not connected,Disable Data c. Go to step 1 And these steps 1 to 3 are getting executed in For loop for User Given number of retries. Now my problem is: I'm stuck at step No. 2. I'm unable to make waitfor(int seconds) function. I tried using Runnable PostDelayed

Simple Swing Delay

孤人 提交于 2019-12-17 22:01:08
问题 In a swing application, I have a popup jDialog which pops up with a jlabel that says "Hang on 5 seconds." After 5 Seconds, the label should change to "Okay, now I'm done." And a button should appear allowing the user to click continue. In the example action below (linked to a button which causes the popup), the popup appears as it should but it is blank instead of saying "Hang on 5 seconds." Then after 5 seconds everything updates and the labels are there and the button too. So what's going

Delay is not working in java graphics

五迷三道 提交于 2019-12-17 20:42:05
问题 This is a code for drawing points on calculated locations by Bresenham's algorithm: public void drawBresenhamPoints(Graphics2D g2, List<Point> bresenham) throws InterruptedException { Graphics2D g = (Graphics2D) g2; if(bresenham == null) return; g.setColor(Color.DARK_GRAY); for(int i = 0; i < bresenham.size(); i = i+20) { int x = bresenham.get(i).x - pointWidth1/2; int y = bresenham.get(i).y - pointWidth1/2; int ovalW = pointWidth1; int ovalH = pointWidth1; g.fillOval(x, y, ovalW, ovalH); //

How to suspend a java thread for a small period of time, like 100 nanoseconds?

余生颓废 提交于 2019-12-17 18:50:13
问题 I know Thread.sleep() can make a java thread suspend for a while, like certain milliseconds and certain nanoseconds. But the problem is the invocation of this function also causes overhead. For example, if I want a thread to suspend for 100 nanoseconds, and I call Thread.sleep(0, 100) . The whole cost for this process is invocation_cost + 100 nanosceonds , which may be much larger the what I want. How could I avoid this problem, and achieve my purpose? The reason I need this is that I want to

Java Button pausing graphical updates

白昼怎懂夜的黑 提交于 2019-12-17 14:55:54
问题 So I have a class where I have to make a program to make Simon. I know the way I'm doing it is not necessarily the best way, However, he had some obscure requirements so that is why I'm doing it this way. My program is close to finished, but I have one MAJOR issue. When I press the reset button I call a method called reset which in turn sets the computer to play their first move. During this, there are graphical updates. When I call the reset method by itself it works as expected When I press

SwingWorker does not update JProgressBar without Thread.sleep() in custom dialog panel

☆樱花仙子☆ 提交于 2019-12-17 07:53:10
问题 I have a SwingWorker class which loads a text file and slices it to chunks for further processing. This is the SwingWorker class: public class ConverterWorker extends SwingWorker<String, String> { private final File f; private final JLabel label; public ConverterWorker(File f, JLabel label) { this.f = f; this.label = label; } @Override protected String doInBackground() throws Exception { NMTMain.convertableData = getDataSets(f); if(!NMTMain.convertableData.isEmpty()) { return "Done"; } else {

How do I make a delay in Java?

僤鯓⒐⒋嵵緔 提交于 2019-12-16 20:13:10
问题 I am trying to do something in Java and I need something to wait / delay for an amount of seconds in a while loop. while (true) { if (i == 3) { i = 0; } ceva[i].setSelected(true); // I need to wait here ceva[i].setSelected(false); // I need to wait here i++; } I want to build a step sequencer and I'm new to Java. Any suggestions? 回答1: If you want to pause then use java.util.concurrent.TimeUnit : TimeUnit.SECONDS.sleep(1); To sleep for one second or TimeUnit.MINUTES.sleep(1); To sleep for a

Is it clearer to sleep near a function call in a loop or in the function call itself?

你离开我真会死。 提交于 2019-12-13 18:43:25
问题 Is it clearer to sleep near a function call in a loop or in the function call itself? Personally I lean toward sleeping near the call rather than in the call, because there is nothing about "getApple()" that implies that it should sleep for some amount of time before returning the apple. I think it would be clearer to have: for ( int i = 0; i < 10; ++i ) { getApple(); sleep() } than... for ( int i = 0; i < 10; ++i ) { getApple(); } Apple getApple() { sleep(1); return new Apple(); } Of course,