ReportProgress是否必须在c#中的DoWork后台方法中完成?
我有一个收集视频列表数据的应用程序。我希望进度和文件名能够实时更新。我是否必须将检查这些文件的所有代码都放在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,代码不必实际存在,但您必须从 DoWork 中调用代码。目前您的 DoWork 方法没有执行任何操作。
另请记住设置属性
WorkerReportsProgress
在您的BackgroundWorker
上设置为true
。您可以在设计器中执行此操作。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
totrue
on yourBackgroundWorker
. You can do this in the designer.这是整数除法,1 / 2 = 0,而不是 0.5。由于 counter 永远不会大于 Count,因此表达式始终生成 0。一种可能的解决方法是计算百分比:
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: