定期报告BackgroundWorker的进度

发布于 2024-12-22 21:49:44 字数 771 浏览 3 评论 0原文

我正在写一个音乐播放器。这是将目录添加到播放列表的(早期)代码:

    private void SelectFolderButton_Click(object sender, EventArgs e)
    {
        int count = 0;
        AddFolderDialog.ShowDialog();
        if(AddFolderDialog.SelectedPath != string.Empty)
        {
            BackgroundWorker bgw = new BackgroundWorker();
            bgw.DoWork += (a,b) => playlist.AddFolder(AddFolderDialog.SelectedPath, RecursiveCheckBox.Checked, out count);
            bgw.RunWorkerAsync();
            bgw.RunWorkerCompleted += (a, b) => mainStatusLabel.Text = "Added " + count + " songs"; ;
            bgw.RunWorkerCompleted += (a, b) => DrawPlaylist();
        }
    }

我刚刚开始使用线程。第一个问题是,这是正确的代码吗?这里有什么明显的错误吗?第二个问题是我想定期显示添加的歌曲数量。不一定是逐首歌;每秒一次就可以了。我该如何实现这一目标?

I'm writing a music player. This is the (early) code that adds a directory to the playlist:

    private void SelectFolderButton_Click(object sender, EventArgs e)
    {
        int count = 0;
        AddFolderDialog.ShowDialog();
        if(AddFolderDialog.SelectedPath != string.Empty)
        {
            BackgroundWorker bgw = new BackgroundWorker();
            bgw.DoWork += (a,b) => playlist.AddFolder(AddFolderDialog.SelectedPath, RecursiveCheckBox.Checked, out count);
            bgw.RunWorkerAsync();
            bgw.RunWorkerCompleted += (a, b) => mainStatusLabel.Text = "Added " + count + " songs"; ;
            bgw.RunWorkerCompleted += (a, b) => DrawPlaylist();
        }
    }

I just started using threads. The first question is, is this a correct code? Is there something glaringly wrong here? The second issue is that I want to regularly display the number of added songs as they're being added. Not necessarily song-by-song; once a second is fine. How do I achieve this?

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

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

发布评论

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

评论(2

被你宠の有点坏 2024-12-29 21:49:44

看看这个SO线程。我通过评论为另一位用户回答了这个问题。
从不同线程更新 MainWindow 中的进度条< /a>

本质上,您缺少后台工作程序中的 WorkerReportsProgress = true 以及处理 ProgressChanged 事件。另外,在设置所有事件之后放置 RunWorkerAsync

如果您希望定期更新,则应该使用 DispatcherTimer,并将方法放在 Tick 事件中。

Check out this SO thread. I answered that for another user with comments.
Update progress bar in MainWindow from a different Thread

Essentially, you are missing WorkerReportsProgress = true in your background worker, as well as handling the ProgressChanged event. Also, put RunWorkerAsync after you set all your events.

If you are looking at updating on regular intervals, you should use the DispatcherTimer instead, and place your methods within the Tick event.

半﹌身腐败 2024-12-29 21:49:44

Backgroundworker 可帮助您保持 UI 响应灵敏且易于使用。它还为您关心多线程的事情。

Backgroundworker 也有一个 ProgressChanged 事件。工作线程可以使用 ReportProgress(int percentProgress) 方法报告进度。如果您不知道百分比,这也没有问题;只需将处理的歌曲数量作为参数传递即可。它是您的 ProgressChanged 事件处理程序,它接收此数字。让它根据这个数字做任何适当的事情。在处理一定数量的歌曲后或经过一定时间后引发 ProgressChanged 事件。只需记住开始时间或上次进度更改的时间,并查看此后已经过去了多少时间。不需要任何定时器之类的。

另请注意,多线程的一个基本问题是,只有 UI 线程(运行主应用程序代码的线程)才允许与表单和控件进行交互。 Backgroundworker 自动为您调用 UI 线程上的 RunWorkerCompleted 和 ProgressChanged 事件处理程序。

The Backgroundworker helps you to keep your UI responsive and is easy to use. It also cares for the multithreading stuff for you.

The Backgroundworker has a ProgressChanged event as well. The working thread can report the progress with the ReportProgress(int percentProgress) method. If you do not know the percentage this is not problem; just pass the number of songs processed as parameter. It is your ProgressChanged event handler, which receives this number. Let it do whatever is appropriate with this number. Raise the ProgressChanged event after a certain number of songs have been processed or after a certain time has elapsed. Just remember the start-time or the time of the last progress change and look how much time has elapsed since. No need for any timer or the like.

Note also that a fundamental problem with multithreading is, that only the UI-thread (the one your main application code runs in) is allowed to interact with forms and controls. The Backgroundworker automatically calls the RunWorkerCompleted and the ProgressChanged event handlers on the UI-thread for you.

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