标签: timer

你如何在C#中使用Timers在做某事之前等待一段时间?

我被告知,与使用Thread.Sleep(int)程序在继续之前等待一段时间相反,我应该使用计时器.

我想知道应该怎么做?有人可以给我一个简短的例子吗?

.net c# timer

-1
推荐指数
1
解决办法
2万
查看次数

是否有其他方法可以设置长定时器间隔

我正在制作一个程序,必须每30或60分钟检查一次数据库,并在Windows窗体界面中显示结果(如果有的话).当然,在执行数据库检查时,from提供访问的其他功能仍然可用.为此,我使用的是System.Timers.Timer,它在与UI不同的线程上执行一个方法(如果使用这种方法有问题,请随时评论它).我写了一个小而简单的程序,以测试热门的工作,只是注意到我不能真正设置Interval超过1分钟(我需要30分钟到一个小时).我想出了这个解决方案:

public partial class Form1 : Form
{

    int s = 2;

    int counter = 1; //minutes counter

    System.Timers.Timer t;

    public Form1()
    {
        InitializeComponent();

        t = new System.Timers.Timer();
        t.Elapsed += timerElapsed;
        t.Interval = 60000;
        t.Start();
        listBox1.Items.Add(DateTime.Now.ToString());
    }


    //doing stuff on a worker thread
    public void timerElapsed(object sender, EventArgs e)
    {
        //check of 30 minutes have passed
        if (counter < 30)
        {
            //increment counter and leave method
            counter++;
            return;
        }
        else
        {
            //do the stuff
            s++;
            string result = s + …
Run Code Online (Sandbox Code Playgroud)

.net c# timer intervals

-1
推荐指数
1
解决办法
9712
查看次数

Swing计时器的基本功能

我是java平面设计的新手,如果可能的话,我希望你帮我一个简单的例子来帮助我理解JFrames,Timers,SwingControllers和所有这些东西的基本功能.您将如何实施以下案例:

我们有一个内置JPanel的JFrame.执行开始时,JPanel是白色的,但我们希望它每两秒更改一次颜色:

public class MiJFrame extends javax.swing.JFrame {

    public MiJFrame() {
        initComponents();
    }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MiJFrame().setVisible(true);
                jPanel1.setBackground(Color.yellow);
                jPanel1.setBackground(Color.RED);
            }
        });
    }

    // Variables declaration - do not modify
    private static javax.swing.JPanel jPanel1;
    // End of variables declaration
}
Run Code Online (Sandbox Code Playgroud)

首先,我在setBackgroud()方法之间使用了线程对象的sleep方法,但它不起作用,因为它只显示最后一次更改.你如何在这里使用Timer对象?

java swing timer awt jframe

-1
推荐指数
1
解决办法
366
查看次数

Java中的计时器

我正在尝试Thread.sleep()用java swing计时器替换,因为我听说这对于图形更好.

之前,我有这样的设置,但它干扰了图形.

while(counter < array.size){
Thread.sleep(array.get(counter).startTime);
//do first task
Thread.sleep(array.get(counter).secondTime);
//do second task
Thread.sleep(array.get(counter).thirdTime);
//do third task
counter++
}
Run Code Online (Sandbox Code Playgroud)

现在,我正在尝试Thread.sleep()用其中一个替换每个,然后我有在此之后发生的实际事件,但它似乎根本没有等待.

int test = array.get(counter).time;
ActionListener taskPerformer = new ActionListener(){
public void actionPerformed(ActionEvent evt){
}
};
Timer t = new Timer(test, taskPerformer);
t.setRepeats(false);
t.start();
Run Code Online (Sandbox Code Playgroud)

基本上,我如何确保程序等待而不给它任何代码在计时器内执行?谢谢!

java swing multithreading sleep timer

-1
推荐指数
1
解决办法
686
查看次数

如何定期执行一个函数

如何连续每40秒执行一次C#方法?

c# timer

-1
推荐指数
1
解决办法
719
查看次数

在java中设置两个if循环之间的时间

嗨我想在两个条件之间设置几秒钟的时间间隔.在我的编码中,第一个"if"条件执行,控制将停止3秒后,只有第二个"if"条件将执行.我不想使用线程线程概念.我想在Java中使用其他一些选项,如"Timer"类等.我尝试了很多次,但无法找到解决方案.请给出解决方案

提前致谢

这是我的代码:

package com.example;

public class TimeBetween {

   public static void main(String[] args) {
      int a=5;
      int b=3;
      int c;
      if(a!=0&&b!=0) {
         c=a+b;
         System.out.println("Addition:"+c);
      }
      if(a!=0&&b!=0) {
         c=a-b;
         System.out.println("Subtraction:"+c);
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

java timer

-1
推荐指数
1
解决办法
218
查看次数

Thread.pause的错误

这是我的代码:

private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {                                      
    this.jButton5.setText("VietNam");
    try {
        Thread.sleep(10000);
    } catch (InterruptedException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    this.jButton5.setText("Ha Noi");
}
Run Code Online (Sandbox Code Playgroud)

我希望按钮显示"越南",一分钟后它将变为"Ha Noi".但是用我的代码,我的按钮没有显示任何内容,1分钟后显示"Ha Noi",没有"越南"出现一秒钟.

我该如何解决这个问题?我不想使用swing.Timer,因为它无法完全暂停系统.如果有办法解决它,请告诉我.

非常感谢你们.

java swing timer event-dispatch-thread thread-sleep

-1
推荐指数
1
解决办法
73
查看次数

依靠溢出是危险的吗?

以下是我在固件中找到的延迟功能.它看起来有点危险,或者至少让读者感到困惑.

全局变量

static int32u masterTimerCounter; // 32-bit unsigned timer counter
Run Code Online (Sandbox Code Playgroud)

系统勾选中断处理程序

/* Called Every Millisecond */
void sysTickIrqHandler(void)
{
    masterTimerCounter++;
}
Run Code Online (Sandbox Code Playgroud)

设置定时器到期功能

void setTimerExpiration(int32u *timerExpiration, int32u delay)
{
    *timerExpiration = masterTimerCounter + delay;
}
Run Code Online (Sandbox Code Playgroud)

检查定时器是否过期功能

boolean timerExpired(int32u timerExpiration, int32u delay)
{
    if((masterTimerCounter - timerExpiration) < delay)
        return TRUE; // Timer has expired
    else
        return FALSE; // Timer still active
}
Run Code Online (Sandbox Code Playgroud)

设置定时器提取和阻止直到定时器到期

int32u timerExpiration;
setTimerExpiration(&timerExpiration, 15); // Set expiration timer to 15 milliseconds
while(!timerExpired(timerExpiration, 15) // Block until timer has expired …
Run Code Online (Sandbox Code Playgroud)

c c++ counter timer delay

-1
推荐指数
1
解决办法
214
查看次数

async/await with thread timer

我能够通过输入表单Stephen Cleary解决问题.请参阅更新3.

我有一个Windows窗体应用程序,其中包含一个带有async修饰符的方法.如果在按钮的click事件中调用该方法,则它不会阻止UI线程.但是,当我在计时器内调用它作为回调时,它会冻结UI.我无法弄清楚我在这里做错了什么.请看下面我的代码.这只是一个用于演示目的的示例项目.

public Form1()
{
    InitializeComponent();            
}

private async void formCloseAsync()
{
    shutdown stf = new shutdown();
    stf.StartPosition = FormStartPosition.CenterScreen;
    stf.Show();
    var task = Task.Factory.StartNew(processClose);
    await task;
}

private void processClose()
{
    Thread.Sleep(5000);
    Environment.Exit(1);
}

private void simpleButtonAsync_Click(object sender, EventArgs e)
{
    formCloseAsync();
}        

private void _simpleButtonTimer_Click(object sender, EventArgs e)
{
    Timer _shutdownTimer = new Timer(delegate
    {
       formCloseAsync();
    }, null, 5000, Timeout.Infinite);
}
Run Code Online (Sandbox Code Playgroud)

更新1:谢谢大家的宝贵意见.请参阅下面的更新代码

public Form1()
    {
        InitializeComponent();

    }

    private Timer _shutdownTimer;
    private void formCloseAsync()
    {
        shutdown stf = …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous timer winforms

-1
推荐指数
1
解决办法
3310
查看次数

Visual Studio 2015 c#计时器问题

嘿,只是想知道是否有人可以帮助我解决我在Windows窗体应用程序中遇到计时器的问题.这是我正在使用的代码:

private void game_Timer_Tick(object sender, EventArgs e)
    {
        while (true)
        {
            int count = 0;
            count++;
            timeLabel.Text = TimeSpan.FromSeconds(count).ToString();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,每当适用的窗口打开,然后没有任何反应,我无法做任何事情.删除此代码时,窗口工作正常,所以我不确定为什么它不适用于这部分代码.有什么想法吗?谢谢

c# timer winforms

-1
推荐指数
1
解决办法
167
查看次数