我想计算执行程序某些部分所花费的时间(以毫秒为单位).我一直在网上看,但关于这个主题的信息不多.你们任何人都知道怎么做?
我希望能够做的是使用输入询问用户问题.例如:
print('some scenario')
prompt = input("You have 10 seconds to choose the correct answer...\n")
Run Code Online (Sandbox Code Playgroud)
然后如果时间流逝打印出类似的东西
print('Sorry, times up.')
Run Code Online (Sandbox Code Playgroud)
任何指导我正确方向的帮助将不胜感激.
我有一个计时器,viewWillAppear在调用方法时触发,并在调用viewDidDisappear方法时无效.但是在视图之间进行一定量的切换之后,计时器即使在无效之后也会继续触发.有什么问题?
这是我的代码:
NSTimer *timer;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
timer = [NSTimer scheduledTimerWithTimeInterval: 0.2f
target: self
selector:@selector( timerAction )
userInfo:nil
repeats:YES];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[timer invalidate];
timer = nil;
}
-(void) timerAction
{
NSLog(@"timerAction");
}
Run Code Online (Sandbox Code Playgroud) 我已经看过每一个讨论和我可以找到的关于让它工作的线程,但事实并非如此.我有一个更新文本视图的简单计时器(下例中的mTimeTextField).正在正确执行mUpdateTimeTask运行方法(每秒),但UI /文本字段未更新.
我有基于以下信息的代码:
http://android-developers.blogspot.com/2007/11/stitch-in-time.html http://developer.android.com/resources/articles/timed-ui-updates.html
这是代码:
package com.something.handlertest;
import com.something.handlertest.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Test extends Activity {
private Handler mHandler = new Handler();
private int labelNo = 0;
private long currTime = 0L;
private long mStartTime = 0L;
TextView mTimeTextField;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTimeTextField = (TextView)findViewById(R.id.timeTextFieldl);
Button startButton = (Button)findViewById(R.id.start_button);
startButton.setOnClickListener(new …Run Code Online (Sandbox Code Playgroud) 假设有人(邪恶)为我们设置了一个计时器setInterval,但是我们不知道它的ID(我们没有对该对象的引用,setInterval正在返回,也没有它的值)
(function(){
setInterval(function(){console.log('pwned')},
10000)
})();
Run Code Online (Sandbox Code Playgroud)
有办法,如何清除它?是否有可能以其他方式访问计时器?或者至少特别是浏览器/ javascript引擎?
大卫·弗拉纳根(David Flanagan)接触了他的大型JSTDG.
setInterval() method, use in malicious code索引中的键指向
...某些浏览器会检测重复的对话框和长时间运行的脚本,并为用户提供停止它们的选项.但是恶意代码可以使用setInterval()等方法来加载CPU,也可以通过分配大量内存来攻击你的系统.Web浏览器没有一般的方法可以阻止这种火腿攻击.在实践中,这不是Web上的常见问题,因为没有人返回到从事此类脚本滥用的网站!
我有一个简单的程序,一个TextView和两个按钮:Button1和Button2.
单击按钮1将启动计数器,每1秒增加1并在TextView上显示结果; 按钮2上的叮当声将使其停止.这是我的Button1代码的一部分.但它不起作用.
Timer T=new Timer();
T.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
myTextView.setText("count="+count);
count++;
}
}, 1000, 1000);
Run Code Online (Sandbox Code Playgroud)
我知道使用Thread有一些类似的问题,但似乎他们没有提到停止计数器.
任何建议都非常感谢.
添加:
您好,我刚从一个更大的程序中缩短了我的代码,但它仍然崩溃了:
package com.example.hello;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView myTextView;
int count=0;
Timer T;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView=(TextView)findViewById(R.id.t);
T=new Timer();
T.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
myTextView.setText("count="+count);
count++;
}
}, 1000, 1000);
}
@Override
public boolean onCreateOptionsMenu(Menu …Run Code Online (Sandbox Code Playgroud) 我想每隔几秒更换一次图像,这是我的代码:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>change picture</title>
<script type = "text/javascript">
function changeImage()
{
var img = document.getElementById("img");
img.src = images[x];
x++;
if(x >= images.length){
x = 0;
}
var timerid = setInterval(changeImage(), 1000);
} }
var images = [], x = 0;
images[0] = "image1.jpg";
images[1] = "image2.jpg";
images[2] = "image3.jpg";
</script>
</head>
<body onload = "changeImage()">
<img id="img" src="startpicture.jpg">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的问题是它停留在第一张照片上! …
我真的很挣扎.我正在winformsvisual studio中创建一个应用程序,并且需要一个每半小时打一次的后台计时器 - 这样做的目的是从服务器下载更新.
我尝试了几种不同的方法,但是由于教程/示例不好或者我自己的缺点,它们都失败了C#.我认为到目前为止,向我展示我所尝试过的东西将是浪费时间,因为我认为我的尝试远远不够.
有没有人知道一种清晰简单的方法来实现C#新手很容易理解的异步后台计时器?
我想启动Windows服务,以便在特定时间每天运行一个功能.
我应该考虑采用什么方法来实现这个?定时器还是使用线程?
在本文中:http://msdn.microsoft.com/en-us/magazine/cc164015.aspx作者声明System.Threading.Timer不是线程安全的.
从那时起,在Rich的书"CLR via C#"中,在博客上重复了这一点,但是这一点从来都不合理.
此外,MSDN文档确保"此类型是线程安全的".
1)谁说实话?
2)如果这是原始文章是什么使System.Threading.Timer不是线程安全的,它的包装器System.Timers.Timer如何实现更多线程安全?
谢谢
timer ×10
c# ×3
android ×2
javascript ×2
.net ×1
asynchronous ×1
c ×1
cocoa-touch ×1
handler ×1
html ×1
image ×1
input ×1
iphone ×1
postdelayed ×1
python ×1
python-3.x ×1
setinterval ×1
system ×1
time ×1
unix ×1
winforms ×1