每隔X秒执行指定的功能

时间:2022-10-28 01:24:17

I have a Windows Forms application written in C#. The following function checks whenever printer is online or not:

我有一个用C#编写的Windows窗体应用程序。打印机在线时是否检查以下功能:

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

and updates the image if the printer is offline. Now, how can I execute this function isonline() every 2 seconds so when I unplug the printer, the image displayed on the form (pictureBox1) turns into another one without relaunching the application or doing a manual check? (eg. by pressing "Refresh" button which runs the isonline() function)

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

4 个解决方案

#1


83  

Use System.Windows.Forms.Timer.

使用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();
}

You can call InitTimer() in Form1_Load().

您可以在Form1_Load()中调用InitTimer()。

#2


5  

The most beginner-friendly solution is:

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

Drag a Timer from the Toolbox, give it a Name, set your desired Interval and set "Enabled" to True. Then double click the Timer and Visual Studio (or whatever you are using) will write you the following Code:

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

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

there you go. No need to worry pasting it into the wrong code block or something like this.

你去吧无需担心将其粘贴到错误的代码块或类似的东西中。

#3


3  

You can do this easily by adding a Timer to your form (from the designer) and setting it's Tick-function to run your isonline-function.

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

#4


0  

I've uploaded a Nuget Package that can make it so simple, you can have it from here OpenJobScheduler

我已经上传了一个Nuget Package,可以让它变得如此简单,你可以从这里获得OpenJobScheduler

And here how to start using it

在这里如何开始使用它

using OpenJobScheduler;

var jobScheduler = new JobScheduler(TimeSpan.FromMinutes(8), new Action(() => {
  //What you want to execute
}));

jobScheduler.Start(); // To Start up the Scheduler

jobScheduler.Stop(); // To Stop Scheduler from Running.

#1


83  

Use System.Windows.Forms.Timer.

使用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();
}

You can call InitTimer() in Form1_Load().

您可以在Form1_Load()中调用InitTimer()。

#2


5  

The most beginner-friendly solution is:

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

Drag a Timer from the Toolbox, give it a Name, set your desired Interval and set "Enabled" to True. Then double click the Timer and Visual Studio (or whatever you are using) will write you the following Code:

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

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

there you go. No need to worry pasting it into the wrong code block or something like this.

你去吧无需担心将其粘贴到错误的代码块或类似的东西中。

#3


3  

You can do this easily by adding a Timer to your form (from the designer) and setting it's Tick-function to run your isonline-function.

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

#4


0  

I've uploaded a Nuget Package that can make it so simple, you can have it from here OpenJobScheduler

我已经上传了一个Nuget Package,可以让它变得如此简单,你可以从这里获得OpenJobScheduler

And here how to start using it

在这里如何开始使用它

using OpenJobScheduler;

var jobScheduler = new JobScheduler(TimeSpan.FromMinutes(8), new Action(() => {
  //What you want to execute
}));

jobScheduler.Start(); // To Start up the Scheduler

jobScheduler.Stop(); // To Stop Scheduler from Running.