嘿,我是 C# 新手,只想手动运行计时器!所以我只想知道我的代码做错了什么。我只需要在我的计时器中显示一条简单的消息!我的代码是:
public partial class Form1 : Form
{
System.Timers.Timer time;
public Form1()
{
InitializeComponent();
time = new System.Timers.Timer();
time.Interval = 10;
time.Enabled = true;
time.Start();
}
private void time_Tick(object e, EventArgs ea)
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我做错了什么,请让我知道提前谢谢!
BOOL QueryPerformanceCounter(
__out LARGE_INTEGER *lpPerformanceCount
);
LARGE_IN
TEGER startTimer()
{
LARGE_INTEGER start;
DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0);
QueryPerformanceCounter(&start);
SetThreadAffinityMask(GetCurrentThread(), oldmask);
return
start;
}
LARGE_INTEGER endTimer()
{
LARGE_INTE
GER stop;
DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0);
QueryPerformanceCounter(&stop);
SetThreadAffinityMask(GetCurrentThread(), oldmask);
return
stop;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用这些函数,但我不确定它返回值的类型。结束计时器 - 开始计时器 = ? 如何将此结果转换为秒?
我想要开始按钮开始计时,停止按钮在那个时候完全停止它,重置按钮让时钟回到“00.00”。每次我点击重置它都会重新启动时间。我只是想让它重置回“00.00”。谢谢你的帮助。这是我的代码:
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class TimeLogActivity extends ActionBarActivity {Chronometer focus;
Button start, stop, reset;
private Toolbar toolbar;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_log);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
start = (Button) findViewById(R.id.timeLogStartButton);
stop = (Button) findViewById(R.id.timeLogStopButton);
reset = (Button) findViewById(R.id.timeLogResetButton);
focus = (Chronometer) findViewById(R.id.timeLogChronometer);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method …Run Code Online (Sandbox Code Playgroud) 所以我对从头开始编程还很陌生,到目前为止主要使用了几年统一,所以我的一般编程知识非常好。暑假后开始在大学学习游戏开发,但我们从头开始编程,作为一项任务,我们必须在课堂上共同制作的 2D 引擎中制作一个简单的游戏。
所以我决定制作的游戏是炸弹人的副本,并且我已经达到了我现在使炸弹起作用的程度。
我遇到的问题是我不知道如何正确添加一个计时器来倒计时炸弹爆炸的时间,以便玩家可以避免它。
我已经尝试过 SDL_Delay 和 _sleep,它们都只是暂停整个程序,因此不起作用,我已经四处寻找更多选项,但并没有真正理解事情是如何工作的。如果我能得到一些示例和页面链接来解释如何正确地制作这样的东西(一些简单而小巧的东西:P),那么我将不胜感激!
请注意,我们在引擎中使用 SDL。
谢谢!
我有两个JFrame A和B.我有一个gif在框架A上运行.经过一段时间我想关闭框架A和显示框架B.我使用下面的代码但它会在程序执行后立即触发操作.我该如何解决这个问题?
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
new FrameB().setVisible(true);
}
};
Timer timer = new Timer(100 ,taskPerformer);
timer.setRepeats(false);
timer.start();
Run Code Online (Sandbox Code Playgroud) 我使用Timer类每x秒执行一些代码.当此代码中发生某些事情时,我想使计时器无效,从而停止"循环".
我通过执行以下操作设法完成此操作:
let timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { timer in
//when something happens
timer.invalidate()
})
Run Code Online (Sandbox Code Playgroud)
但是,我不在我的代码中的任何其他位置使用此计时器,因此Xcode向我发出警告:
永远不会使用不可变值'timer'的初始化; 考虑将赋值替换为'_'或删除它
显然,这并不能阻止我的应用程序运行,但我想摆脱警告.
我知道只是使用:
Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { timer in
//do stuff
})
Run Code Online (Sandbox Code Playgroud)
仍然会工作,但后来我不知道如何使计时器无效.只是invalidate()在封闭内部调用不起作用(我知道,但我还是试了很久).
通常,当Timer在SO上查看问题时,人们正在使用全局计时器变量,这使得它很容易失效.但是,我不想这样做,因为它没有必要,因为再次,我没有使用计时器对象一旦完成.
我环顾四周,认为这是一个相当普遍的问题,但我找不到任何有用的东西.谁能说明如何在不设置变量的情况下有效地使计时器失效?
我在Python 3.5中遇到了PyQt5的奇怪问题。我有两个课,FrontEnd(QWidget)和TimerThread(Thread)。我QLabel在的init函数中定义了多个FrontEnd,所有都可以正常工作。
显示的是以下几个相关功能FrontEnd:
def update_ui(self):
ret, frame = self.cam_capture.read()
if self.results_pending:
if not path.isfile('output.jpg'):
self.results_pending = False
with open('.out') as content_file:
content = content_file.readlines()[2:-2]
system('rm .out')
self.handle_image_classification(content)
if self.take_picture:
cv2.imwrite('output.jpg', frame)
self.user_prompt.setText('Please wait...')
system('./classifyimage.py --mean mean.binaryproto --nogpu --labels labels.txt model.caffemodel deploy.prototxt output.jpg > .out && rm output.jpg')
self.take_picture = False
self.results_pending = True
image = QImage(frame, frame.shape[1], frame.shape[0], QImage.Format_RGB888).rgbSwapped()
pix = QPixmap.fromImage(image)
self.video_frame.setPixmap(pix)
def update_bar_graph(self, data):
palette = QPalette()
palette.setColor(QPalette.Background, …Run Code Online (Sandbox Code Playgroud) 我有一个STM32F302CBT6(运行在72MHz)项目,我需要测量4个信号的频率,每个信号约250kHz.信号连接到TIM1通道1-4.
现在,了解250kHz太快(或者是?)同时处理所有那些输入捕获中断(因为它们可能同步或同时发生......)我想逐个测量每个通道.我在程序开始时初始化了所有通道,并考虑在每个通道测量后逐个启用相应的中断.这是一个合适的想法还是我错过了什么?
问题是在为通道1提供第一个中断之后,下一个中断从未得到服务,因为虽然没有使能中断,但状态寄存器还有多个其他待处理中断(CCxIF和CCXOF,以及CxIF)以及设置的过捕获标志.我试图通过读取所有捕获值或设置TIMx-> SR = 0但没有帮助来避免此问题.
我将如何测量这些信号以及确保正确捕获每个通道的正确方法是什么?
我对此非常感兴趣,并希望了解如何进行这种处理或者如果你能指出我做错了什么.谢谢.
我目前的相关代码如下.
这是中断处理程序:
void TIM1_CC_IRQHandler(void) {
if (TIM_GetITStatus(IC_TIMER, IC_CH1) == SET) {
/* Clear TIM1 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(IC_TIMER, IC_CH1);
//Read the capture value
raw_captures[capture_index] = TIM_GetCapture1(IC_TIMER);
capture_index++;
//Also read the others to avoid overcaptures
TIM_GetCapture2(IC_TIMER);
TIM_GetCapture3(IC_TIMER);
TIM_GetCapture4(IC_TIMER);
if(capture_index == 2) {
TIM_ITConfig(IC_TIMER, IC_CH1, DISABLE);
}
} else if (TIM_GetITStatus(IC_TIMER, IC_CH2 == SET)) {
TIM_ClearITPendingBit(IC_TIMER, IC_CH2);
//Read the capture value
raw_captures[capture_index] = TIM_GetCapture2(IC_TIMER);
capture_index++;
TIM_GetCapture1(IC_TIMER);
TIM_GetCapture3(IC_TIMER);
TIM_GetCapture4(IC_TIMER);
if(capture_index == 4) …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个脚本,在1.5分钟后将所有灯关闭10秒,然后重新打开.
现在,似乎计时器被绕过了.我意识到这可能是因为时间永远不会是90左右.
话虽如此,我不知道如何得到我想要的结果.
我想过InvokeRepeating改用(如注释掉的那样),但那意味着灯光每次都会关闭更长时间.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsTimerTrigger : MonoBehaviour {
private GameObject[] allLights;
private float time = 0.0f;
private float secondTimer = 0.0f;
void Start () {
// Create array of lights
allLights = GameObject.FindGameObjectsWithTag("riddleLights");
//InvokeRepeating("lightsOn", 60.0f, 120.0f);
//InvokeRepeating("lightsOff", 60.10f, 120.10f); // Exponential
}
// Update is called once per frame
void Update () {
time += Time.deltaTime;
if(time%90 == 0)
{
secondTimer = time;
lightsOff();
Debug.Log("Lights off");
}
if (time == …Run Code Online (Sandbox Code Playgroud) 我有一个功能列表和它们各自的间隔.我想同时以其间隔运行每个函数.
在JavaScript中,我写了类似的东西:
maps.forEach(({fn, interval}) => {
setInterval(fn, interval)
})
Run Code Online (Sandbox Code Playgroud)
如何在Golang中实现此功能?