У меня есть приложение Windows Forms, написанное на C #. Следующая функция проверяет, находится ли принтер в сети или нет:
public void isonline()
{
PrinterSettings settings = new PrinterSettings();
if (CheckPrinter(settings.PrinterName) == "offline")
{
pictureBox1.Image = pictureBox1.ErrorImage;
}
}
и обновляет изображение, если принтер находится в автономном режиме. Теперь, как я могу выполнять эту функцию isonline ()
каждые 2 секунды, чтобы, когда я отключил принтер, изображение, отображаемое в форме ( pictureBox1
), превратилось в другое без перезапуска приложения. или делать ручную проверку? (например, нажав кнопку «Обновить», которая запускает функцию isonline ()
)
Поточный:
/// <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;
}