c#ProgressBar问题

标签 c# progress-bar

我一直在寻找一种方法来创建一个漂亮的进度条。 我找到了一些我使用过的代码,如果我使用循环,它会很好用。但是现在我想在一个真正体面的应用程序中使用它,这给我带来了很多麻烦。我有一个在 IMDB 上查找数据的应用程序,因此我连接到 IMDB,例如 500 个电影标题,所以这需要一段时间。所以我想显示一个进度条,其中进度条会随着它查找的每部电影而增长,并提供一些关于电影标题的额外信息。

我使用以下类:

public partial class ProgressDialog : Window, IProgressContext
{
    public ProgressDialog()
    {
        InitializeComponent();
        IconBitmapDecoder ibd = new IconBitmapDecoder(
        new Uri("IMDB.ML.ico", UriKind.RelativeOrAbsolute),
        BitmapCreateOptions.None, BitmapCacheOption.Default);
        this.Icon = ibd.Frames[0];
    }
    public void UpdateProgress(double progress)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Background,
            (SendOrPostCallback)delegate { Progress.SetValue(ProgressBar.ValueProperty, progress); }, null);
    }

    public void UpdateStatus(string status)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Background,
            (SendOrPostCallback)delegate { StatusText.SetValue(TextBlock.TextProperty, status); }, null);
    }

    public void Finish()
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Background,
            (SendOrPostCallback)delegate { Close(); }, null);
    }
}

public interface IProgressContext
{
    void UpdateProgress(double progress);
    void UpdateStatus(string status);
    void Finish();
}

这是使用进度条的 imdb 查找方法,此方法使用已经存在的 xml 列表,因此它仅用于更新数据,而不是添加新电影:

public static void updateIMDBinfo()
    {

        //initialize progressbar
        ProgressDialog myProgressContext = new ProgressDialog();
        myProgressContext.Show();            

        //load old list
        List<Movie> movieList = XML.getMovieList();

        //create new updated list
        List<Movie> newMovieList = new List<Movie>();
        int count = 1;
        foreach (Movie movie in movieList)
        {
            //update progressbar
            myProgressContext.UpdateProgress((double)count / (double)movieList.Count);
            myProgressContext.UpdateStatus(String.Format("Updating movie: '{0}' ({1}/{2})", movie.Title, count, movieList.Count));

            movie.Link = movie.Link.Substring(movie.Link.IndexOf("http://www.imdb.com/title/") + 26, 9);

            //this creates a new movie where it looks up the data from imdb
            newMovieList.Add(new Movie(movie.Title, movie.Link, movie.Path));

            count++;
        }

        //sort the list
        newMovieList.Sort((Movie m1, Movie m2) => m1.Title.CompareTo(m2.Title));

        //save as xml
        XML.updateMovieList(newMovieList);

        //finish progressbar
        myProgressContext.Finish();
    }

所以现在,当我使用该方法时,它会打开进度条,但进度条永远不会填满,它只会不断向列表中添加电影并查找更新的信息,而不会填满进度条。我认为通过在不同的威胁中使用它可以解决这个问题?

有什么想法吗?

非常感谢

编辑:

我尝试使用 BackgroundWorker。我在我的方法中添加了一个后台 worker 并返回了一个 ReportProgress。我还在我的主类中添加了一个 backgroundWorker,它使用该方法和事件处理程序。但是它没有用。我对不同的威胁不是很熟悉。那我该怎么办呢? 我在这里试过:

http://msdn.microsoft.com/en-us/library/cc221403(VS.95).aspx

最佳答案

由于你和UI在同一个线程,所以进度条不刷新,实际上整个窗口都没有刷新,因为你的操作阻塞了UI主线程。

查看BackgroundWorker用于加载您的电影列表的组件,它包含您需要的一切。

关于c#ProgressBar问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1946377/

相关文章:

c# - backgroundworker+wpf -> 卡住窗口

c# - 什么情况下TypeBuilder.CreateType可以返回null?

c# - 在哪里可以找到 Win32 API 中 ManagementObjectSearcher 中使用的所有表

c# - C# 结构应该如何通过网络发送到 C++ 客户端?

java - 加载另一个 JFrame 类的进度条

javascript - Progressbar.js 结合两种效果

c# - HRESULT 异常 : 0x800A03EC Error

c# - 如何传递附加参数进行分页

Android 如何更新 ProgressBar 的值?

javascript - 在屏幕上可见时加载技能栏