gpt4 book ai didi

c# - ReportProgress 是否必须在c# 的DoWork Ba​​ckground 方法中完成?

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:20 27 4
gpt4 key购买 nike

我有一个应用程序可以收集视频列表中的数据。我希望实时更新进度和文件名。我是否必须将检查这些文件的所有代码都放在 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;

}

最佳答案

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

不,代码不必实际存在,但您必须从 DoWork 中调用代码。目前您的 DoWork 方法没有做任何事情。

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

关于c# - ReportProgress 是否必须在c# 的DoWork Ba​​ckground 方法中完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4420180/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com