壁纸循环器上的计时器

发布于 2024-07-13 02:27:13 字数 684 浏览 11 评论 0原文

我刚刚向 Coding4Fun 项目添加了一些额外的功能。 我的项目设置了一个额外的选项,允许它在 X 时间后自动更改背景。 X 是从 ComboBox 设置的。 但是,我知道我这样做的方式很糟糕,因为我创建了一个以 System.Timers.Timer 作为父级的新计时器类,因此当调用 ElapsedEventHandler 中的静态方法时,我能够返回到窗体并调用 ChangeDesktopBackground()。

以用户定义的时间间隔调用 ChangeDesktopBackground() 的更好方法是什么?

这是我当前的解决方案,其中涉及将发送者转换为继承的计时器,然后获取对表单的引用,然后调用 ChangeDesktopBackground 方法。

private static void timerEvent(object sender, System.Timers.ElapsedEventArgs e)
{
    ((newTimer)sender).getCycleSettingsForm().ChangeDesktopBackground();
}

编辑:添加编码示例以显示当前解决方案

I just added some extra functionality to a Coding4Fun project. I have my project set up with an extra option to allow it to automatically change the background after X amount of time. X is set from a ComboBox. However, I know I've done this in a terrible way, as I have created a new timer class with System.Timers.Timer as a parent so when the static method in the ElapsedEventHandler is called, I'm able to get back to the form and call ChangeDesktopBackground().

What is a better way to call ChangeDesktopBackground() at a user defined interval?

Here is my current solution, which involves me casting the sender as my inherited timer, which then gets a reference to the form, which then calls the ChangeDesktopBackground method.

private static void timerEvent(object sender, System.Timers.ElapsedEventArgs e)
{
    ((newTimer)sender).getCycleSettingsForm().ChangeDesktopBackground();
}

Edit:Added coding sample to show current solution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

本王不退位尔等都是臣 2024-07-20 02:27:13

我自己之前写过类似的东西。 System.Timers.Timer 对此来说太过分了。 您可能应该使用 System.Windows.Forms.Timer,原因如下:

  1. 您正在做的事情不必太精确。 Windows 计时器只是发送到 Windows 应用程序消息泵的 WM_TIMER 消息,因此您无法获得超高精度,但每秒更改一次壁纸是不现实的。 (我写的是每 6 小时左右更改一次)
  2. 当使用执行某种基于计时器的任务的 Windows 窗体应用程序时,如果您使用 System.Timers.Timer,您将遇到各种线程关联问题。 任何 Windows 控件都与其创建所在的线程具有关联性,这意味着您只能修改该线程上的控件。 Windows.Forms.Timer 将为您完成所有这些工作。 (对于未来挑剔的人来说,更改壁纸实际上并不算数,因为它是注册表值更改,但规则通常适用)

I've written something like this before myself. System.Timers.Timer is overkill for this. You should probably use System.Windows.Forms.Timer, for a couple of reasons:

  1. You're doing something that doesn't have to be too precise. The Windows timer is just a WM_TIMER message sent to your windows app's message pump, so you're not getting super great precision, but changing your wallpaper once a second is unrealistic. (I wrote mine to change every 6 hours or so)
  2. When using a Windows Forms app that does some kind of timer-based task, you're going to run into all kinds of thread affinity issues if you go with System.Timers.Timer. Any Windows control has an affinity for the thread on which it was created, meaning that you can only modify the control on that thread. A Windows.Forms.Timer will do all that stuff for you. (For future nitpickers, changing wallpaper doesn't really count, cause it's a registry value change, but the rule holds generally)
何以笙箫默 2024-07-20 02:27:13

计时器可能是最直接的方法,尽管我不确定您是否正确使用计时器。 以下是我在项目中使用计时器的方式:

// here we declare the timer that this class will use.
private Timer timer;

//I've shown the timer creation inside the constructor of a main form,
//but it may be done elsewhere depending on your needs
public Main()
{

   // other init stuff omitted

   timer = new Timer();     
   timer.Interval = 10000;  // 10 seconds between images
   timer.Tick += timer_Tick;   // attach the event handler (defined below)
}


void timer_Tick(object sender, EventArgs e)
{
   // this is where you'd show your next image    
}

然后,您将连接 ComboBox onChange 处理程序,以便更改 timer.Interval。

Timers are probably the most straight-forward way of doing it, although I'm not sure you're using a timer correctly. Here's how I've used timers in my projects:

// here we declare the timer that this class will use.
private Timer timer;

//I've shown the timer creation inside the constructor of a main form,
//but it may be done elsewhere depending on your needs
public Main()
{

   // other init stuff omitted

   timer = new Timer();     
   timer.Interval = 10000;  // 10 seconds between images
   timer.Tick += timer_Tick;   // attach the event handler (defined below)
}


void timer_Tick(object sender, EventArgs e)
{
   // this is where you'd show your next image    
}

Then, you'd connect your ComboBox onChange handler such that you'd be changing timer.Interval.

梦与时光遇 2024-07-20 02:27:13

我会为此使用微软的响应式框架。 只是 NuGet“Rx-WinForms”。

代码如下:

var subscription =
    Observable
        .Interval(TimeSpan.FromMinutes(1.0))
        .ObserveOn(this)
        .Subscribe(n => this.getCycleSettingsForm().ChangeDesktopBackground());

要停止它,只需执行 subscription.Dispose() 即可。

简单的。

I would use Microsoft's Reactive Framework for this. Just NuGet "Rx-WinForms".

Here's the code:

var subscription =
    Observable
        .Interval(TimeSpan.FromMinutes(1.0))
        .ObserveOn(this)
        .Subscribe(n => this.getCycleSettingsForm().ChangeDesktopBackground());

To stop it just do subscription.Dispose().

Simple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文