执行特定功能每隔X秒 [英] Execute specified function every X seconds
问题描述
我有一个Windows窗体应用程序用C#编写。下面的函数,只要检查打印机是否联机:
I have a Windows Forms application written in C#. The following function checks whenever printer is online or not:
public void isonline()
{
PrinterSettings settings = new PrinterSettings();
if (CheckPrinter(settings.PrinterName) == "offline")
{
pictureBox1.Image = pictureBox1.ErrorImage;
}
}
和更新的图像,如果打印机处于脱机状态。现在,我怎么能执行此功能 isonline()
每2秒,所以当我拔掉打印机,显示在表单上的图像( pictureBox1
)变成另外一个没有重新推出应用程序或做人工检查? (例如,通过pressing刷新按钮,运行 isonline()
功能)
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)
推荐答案
使用<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx">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()
}
这篇关于执行特定功能每隔X秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!