ReportProgress是否必须在c#中的DoWork后台方法中完成?

发布于 2024-10-07 04:47:18 字数 1831 浏览 4 评论 0原文

我有一个收集视频列表数据的应用程序。我希望进度和文件名能够实时更新。我是否必须将检查这些文件的所有代码都放在 bw_DoWork 方法中?

目前我有这个方法,但 bw.ReportProgress 无法正常工作。

private void GetMovieData(List<FileInfo> completeMovieList)
    {
        List<string> movieLists = new List<string>();

        int counter = 0;
        foreach (FileInfo movie in completeMovieList)
        {
            counter++;
            string[] movieData = new string[3];

            bw.ReportProgress(counter / completeMovieList.Count);


            // get last modified date
            string movieLastModified = string.Concat(movie.LastWriteTime.Year + "/" + movie.LastWriteTime.Month + "/" + movie.LastWriteTime.Day);

            // get duation of video
            string lengthOfVideo = Program.GetDetails(movie);

            if (!movieListWithData.ContainsKey(movie.Name))
            {
                movieData[0] = lengthOfVideo;
                movieData[1] = movieLastModified;
                movieData[2] = movie.FullName;
                movieListWithData.Add(movie.Name, movieData);

            }

            movieLists.Add(movie.FullName + "|" + lengthOfVideo + "|" + movieLastModified);

        }

        StringBuilder sb = new StringBuilder();

        foreach (KeyValuePair<string, string[]> key in movieListWithData)
        {
            sb.AppendLine(key.Key + "|" + key.Value[0] + "|" + key.Value[1] + "|" + key.Value[2]);
        }

        File.WriteAllText(Program.movieTxtFileLocalPath, sb.ToString());
    }

我的 DoWork 方法如下所示:

private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;

            }

}

I have an app that collects data on a list of videos. I want the progress and filenames to be updated in real time. Do I have to place all the code that examines those files in the bw_DoWork method?

Currently I have this method, but the bw.ReportProgress isn't working right.

private void GetMovieData(List<FileInfo> completeMovieList)
    {
        List<string> movieLists = new List<string>();

        int counter = 0;
        foreach (FileInfo movie in completeMovieList)
        {
            counter++;
            string[] movieData = new string[3];

            bw.ReportProgress(counter / completeMovieList.Count);


            // get last modified date
            string movieLastModified = string.Concat(movie.LastWriteTime.Year + "/" + movie.LastWriteTime.Month + "/" + movie.LastWriteTime.Day);

            // get duation of video
            string lengthOfVideo = Program.GetDetails(movie);

            if (!movieListWithData.ContainsKey(movie.Name))
            {
                movieData[0] = lengthOfVideo;
                movieData[1] = movieLastModified;
                movieData[2] = movie.FullName;
                movieListWithData.Add(movie.Name, movieData);

            }

            movieLists.Add(movie.FullName + "|" + lengthOfVideo + "|" + movieLastModified);

        }

        StringBuilder sb = new StringBuilder();

        foreach (KeyValuePair<string, string[]> key in movieListWithData)
        {
            sb.AppendLine(key.Key + "|" + key.Value[0] + "|" + key.Value[1] + "|" + key.Value[2]);
        }

        File.WriteAllText(Program.movieTxtFileLocalPath, sb.ToString());
    }

My DoWork method looks like:

private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;

            }

}

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

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

发布评论

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

评论(2

初心 2024-10-14 04:47:18

我是否必须将检查这些文件的所有代码都放在 bw_DoWork 方法中?

不,代码不必实际存在,但您必须从 DoWork 中调用代码。目前您的 DoWork 方法没有执行任何操作。

另请记住设置属性 WorkerReportsProgress 在您的 BackgroundWorker 上设置为 true。您可以在设计器中执行此操作。

Do I have to place all the code that examines those files in the bw_DoWork method?

No, the code doesn't have to physically be there, but you have to call the code from DoWork. Currently your DoWork method isn't doing anything.

Also remember to set the property WorkerReportsProgress to true on your BackgroundWorker. You can do this in the designer.

隔岸观火 2024-10-14 04:47:18
        bw.ReportProgress(counter / completeMovieList.Count);

这是整数除法,1 / 2 = 0,而不是 0.5。由于 counter 永远不会大于 Count,因此表达式始终生成 0。一种可能的解决方法是计算百分比:

        bw.ReportProgress(100 * counter / completeMovieList.Count);
        bw.ReportProgress(counter / completeMovieList.Count);

That's an integer division, 1 / 2 = 0, not 0.5. Since counter will never be larger than Count, the expression always produces 0. One possible fix is to calculate a percentage:

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