C#:根据后台任务定期更新GUI

发布于 2024-12-11 05:42:52 字数 362 浏览 0 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

翻身的咸鱼 2024-12-18 05:42:52

当我需要执行数据库操作同时仍允许 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

夏至、离别 2024-12-18 05:42:52

使用线程中的事件告诉 UI 某些内容发生了更改:

    // Just detected that that a new file has been created
    if (this.FileCreated_Event != null)
    {
        this.FileCreate_Event(this, new FileEventArgs(newFileName));
    }

其中 FileCreated_EventFileEventArgs 已适当声明。

然后在 UI 中,当您收到事件时,您将看到以下内容:

this.fileChecker.FileCreated_Event += this.FileCreated_Event;

和:

    private void FileCreated_Event(object sender, TrackStatusEventArgs e)
    {
        if ((sender as Control).InvokeRequired)
        {
            (sender as Control).Invoke(action);
        }
        else
        {
            action();
        }
    }

其中 action 是您想要执行的操作。

Use events from the thread to tell the UI that something's changed:

    // Just detected that that a new file has been created
    if (this.FileCreated_Event != null)
    {
        this.FileCreate_Event(this, new FileEventArgs(newFileName));
    }

where FileCreated_Event and FileEventArgs are declared appropriately.

Then in the UI when you receive the event you have the following:

this.fileChecker.FileCreated_Event += this.FileCreated_Event;

and:

    private void FileCreated_Event(object sender, TrackStatusEventArgs e)
    {
        if ((sender as Control).InvokeRequired)
        {
            (sender as Control).Invoke(action);
        }
        else
        {
            action();
        }
    }

where action is the thing you want to do.

烟若柳尘 2024-12-18 05:42:52

尝试教程。最后我确信您将能够使用线程。但您必须小心,因为您必须管理这些线程,这可能是一项艰巨的任务。我从未见过喜欢调试多线程的程序员......

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...

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