后台工作者代表&调用者

发布于 2024-12-12 12:11:51 字数 158 浏览 0 评论 0原文

我有一个后台工作人员执行许多 GUI 交互,但我的问题是所有这些对象都在主线程中(据我所知),并且我需要一直调用所有对象,这使得我的代码更长并且可读性较差。
1) 你知道如何创建一个调用这些 GUI 元素的通用方法吗?
2)有没有更简单的方法来面对这个问题?

提前致谢。

I have a background worker that does many GUI interactions, but my problem is that all of those objects are in the main thread (as I know), and I need to invoke all of the all the time, something that is making my code longer and less readable.
1) Do you know how can I create a generic method that will invoke those GUI elements.
2) Is there a more simple way to confront this?

Thanks in advance.

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

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

发布评论

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

评论(1

遗弃M 2024-12-19 12:11:51

使用BackgroundWorker,您可以ReportProgress并传递完成百分比和一个对象。您可以创建一个类来包含要传递到 UI 的数据,然后将其作为对象传递。

ProgressChanged 事件可以操作 UI 对象,因为它将在 UI 线程中运行。

您的问题示例

    private void Form1_Load(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        List<string> items = new List<string>();

        items.Add("Starting");
        System.Threading.Thread.Sleep(2500);
        backgroundWorker1.ReportProgress(25, items.ToArray());

        items.Add("Halfway there");
        System.Threading.Thread.Sleep(2500);
        backgroundWorker1.ReportProgress(50, items.ToArray());

        items.Add("Almost there");
        System.Threading.Thread.Sleep(2500);
        backgroundWorker1.ReportProgress(75, items.ToArray());

        items.Add("Done");
        System.Threading.Thread.Sleep(2500);
        backgroundWorker1.ReportProgress(100, items.ToArray());
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        listBox1.Items.Clear();
        listBox1.Items.AddRange((string [])e.UserState);
        this.Text = e.ProgressPercentage.ToString();
    }

ReportProgress 中的第二个参数是对象类型。您可以在其中放入任何您想要的内容,然后将其转换为另一侧的正确类型。

With the BackgroundWorker you can ReportProgress and pass a percentage complete and an object. You could create a class to contain the data to pass to UI and then pass it as the object.

The ProgressChanged Event can manipulate UI Objects since it would be running in the UI Thread.

Example for your issue

    private void Form1_Load(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        List<string> items = new List<string>();

        items.Add("Starting");
        System.Threading.Thread.Sleep(2500);
        backgroundWorker1.ReportProgress(25, items.ToArray());

        items.Add("Halfway there");
        System.Threading.Thread.Sleep(2500);
        backgroundWorker1.ReportProgress(50, items.ToArray());

        items.Add("Almost there");
        System.Threading.Thread.Sleep(2500);
        backgroundWorker1.ReportProgress(75, items.ToArray());

        items.Add("Done");
        System.Threading.Thread.Sleep(2500);
        backgroundWorker1.ReportProgress(100, items.ToArray());
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        listBox1.Items.Clear();
        listBox1.Items.AddRange((string [])e.UserState);
        this.Text = e.ProgressPercentage.ToString();
    }

The second argument in ReportProgress is an Object type. you can put whatever you want in it and cast it to the proper type on the other side.

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