壁纸循环器上的计时器
我刚刚向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我自己之前写过类似的东西。 System.Timers.Timer 对此来说太过分了。 您可能应该使用 System.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:
计时器可能是最直接的方法,尽管我不确定您是否正确使用计时器。 以下是我在项目中使用计时器的方式:
然后,您将连接 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:
Then, you'd connect your ComboBox onChange handler such that you'd be changing timer.Interval.
我会为此使用微软的响应式框架。 只是 NuGet“Rx-WinForms”。
代码如下:
要停止它,只需执行
subscription.Dispose()
即可。简单的。
I would use Microsoft's Reactive Framework for this. Just NuGet "Rx-WinForms".
Here's the code:
To stop it just do
subscription.Dispose()
.Simple.