每隔X秒执行指定的功能

时间:2011-05-29 17:37:35

标签: c# .net winforms

我有一个用C#编写的Windows窗体应用程序。无论何时打印机在线,以下功能都会检查:

public void isonline()
{
    PrinterSettings settings = new PrinterSettings();
    if (CheckPrinter(settings.PrinterName) == "offline")
    {
        pictureBox1.Image = pictureBox1.ErrorImage;
    }
}

并在打印机脱机时更新图像。现在,我怎么能每2秒执行一次这个函数isonline()所以当我拔掉打印机时,表格上显示的图像(pictureBox1)变成另一个图像而不重新启动应用程序或进行手动检查? (例如,通过按“刷新”按钮运行isonline()功能)

6 个答案:

答案 0 :(得分:96)

使用System.Windows.Forms.Timer

private Timer timer1; 
public void InitTimer()
{
    timer1 = new Timer();
    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Interval = 2000; // in miliseconds
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    isonline();
}

您可以在InitTimer()中致电Form1_Load()

答案 1 :(得分:5)

最适合初学者的解决方案是:

从工具箱中拖动一个计时器,为其命名,设置所需的间隔,并将“已启用”设置为True。然后双击Timer和Visual Studio(或者您正在使用的任何内容)将为您编写以下代码:

private void wait_Tick(object sender, EventArgs e)
{
    refreshText(); // Add the method you want to call here.
}

无需担心将其粘贴到错误的代码块或类似的内容中。

答案 2 :(得分:3)

您可以通过向表单(从设计人员)添加Timer并将其设置为Tick函数来运行您的isonline函数来轻松完成此操作。

答案 3 :(得分:0)

线程化:

    /// <summary>
    /// Usage: var tmr = SetInterval(DoThis, 1000);
    /// Updating the UI: BeginInvoke((Action)(() =>{ //Code Here; }));
    /// </summary>
    /// <returns>Returns a timer object which can be disposed.</returns>
    public static System.Threading.Timer SetIntervalThread(Action Act, int Interval)
    {
        TimerStateManager state = new TimerStateManager();
        System.Threading.Timer tmr = new System.Threading.Timer(new TimerCallback(_ => Act()), state, Interval, Interval);
        state.TimerObject = tmr;
        return tmr;
    }

常规

    /// <summary>
    /// Usage: var tmr = SetInterval(DoThis, 1000);
    /// Updating the UI: BeginInvoke((Action)(() =>{ //Code Here; }));
    /// </summary>
    /// <returns>Returns a timer object which can be stopped and disposed.</returns>
    public static System.Timers.Timer SetInterval(Action Act, int Interval)
    {
        System.Timers.Timer tmr = new System.Timers.Timer();
        tmr.Elapsed += (sender, args) => Act();
        tmr.AutoReset = true;
        tmr.Interval = Interval;
        tmr.Start();

        return tmr;
    }

答案 4 :(得分:0)

using System;
using System.Timers;
namespace SnirElgabsi
{
  class Program
  {
     private static Timer timer1;
     static void Main(string[] args)
     {
         timer1 = new Timer(); //new Timer(1000);
         timer1.Elpased += (sender,e) =>
         {
            MyFoo();
         }
         timer1.Interval = 1000;//miliseconds
         timer1.Start();
       
         Console.WriteLine("press any key to stop");
         Console.ReadKey();
     }

     private static void MyFoo()
     {
         Console.WriteLine(string.Format("{0}", DateTime.Now));
     }
  }
}

答案 5 :(得分:0)

随着时间的推移,情况发生了很大变化。 您可以使用以下解决方案:

static void Main(string[] args)
{
    var timer = new Timer(Callback, null, 0, 2000);

    //Dispose the timer
    timer.Dispose();
}
static void Callback(object? state)
{
    //Your code here.
}
相关问题