C#:根据后台任务定期更新GUI
我有一个 GUI,它对于所有意图和目的来说都是非常基本的。一个列表视图,一个 html 表单,仅此而已。
我希望用户具有以下行为能力:
1 - 单击“实时”复选框。单击后,后台线程将每 10 秒运行一次。
2 - 如果创建了一个新文件(这很容易,观察一个新文件)我希望在我的主 gui 中显示一个警报。现在显示的位置是任意的(例如在标签中)。
主要问题是我无法弄清楚如何在多线程示例中执行此操作。我的目标与多线程完全一致:执行任务 1 和 2,而不锁定任务 1。这意味着,在运行更新检查时,用户可以与 GUI 交互,就好像后台没有发生任何事情一样。
如果您需要更多详细信息以更好地回答此问题,请告诉我。
谢谢!
I have a GUI that is for all intents and purposes really basic. A listview, an html form, and that's really it.
I want the user to have the following behavioral ability:
1 - Click a checkbox that says "Real-time". When clicked, a background thread will run once every 10 seconds.
2 - If there is a new file created (this is easy, to observe a new file) I want an alert displayed in my main gui. Where it is displayed for now is arbitrary (in a label, for example).
The main issue is I cannot figure out how to do this in a multi-threaded example. My goal is exactly in line with multithreading: do tasks 1 and 2, without locking task 1. Meaning, while the update check is running, the user can interact with the GUI as if nothing was going on in the background.
If you need more details to better answer this please let me know.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我需要执行数据库操作同时仍允许 GUI 响应时,我发现以下几个站点对于实现后台工作人员非常有用:
http://msdn.microsoft.com/en-us/library/zw97wx20.aspx
http://www.codeproject.com/KB/cs/AsynchronousCodeBlocks.aspx
Here are a couple sites I found useful for implementing a background worker when I needed to perform database operations while still allowing the GUI to be responsive:
http://msdn.microsoft.com/en-us/library/zw97wx20.aspx
http://www.codeproject.com/KB/cs/AsynchronousCodeBlocks.aspx
使用线程中的事件告诉 UI 某些内容发生了更改:
其中
FileCreated_Event
和FileEventArgs
已适当声明。然后在 UI 中,当您收到事件时,您将看到以下内容:
和:
其中
action
是您想要执行的操作。Use events from the thread to tell the UI that something's changed:
where
FileCreated_Event
andFileEventArgs
are declared appropriately.Then in the UI when you receive the event you have the following:
and:
where
action
is the thing you want to do.尝试此教程。最后我确信您将能够使用线程。但您必须小心,因为您必须管理这些线程,这可能是一项艰巨的任务。我从未见过喜欢调试多线程的程序员......
Try this tutorial. At the end I'm sure you'll be able to use threads. You must be careful though, because you'll have to manage those threads which can be a daunting task. I've never met a programmer who liked to debug multiple threads...