c#ProgressBar问题 [英] c# ProgressBar problem

查看:56
本文介绍了c#ProgressBar问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was looking for a way to create a nice progressbar. I found some code which I used and it worked great if I used a loop. But now I wanted to use it in a real decent application and it gave me a lot of trouble. I have an application where I lookup data on IMDB, so I make a connection to IMDB for for example 500 movietitles, so this takes a while. So I wanted to show a progressbar where the bar grew for each movie it looked up and with some extra info on the movietitle.

我使用以下类:

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 列表,因此仅用于更新数据,而不是添加新电影:

This is the imdb lookup method which uses the progressbar, this method uses an already existing xml list so it's only for updating data, not adding new movies:

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();
    }

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

So now when i use the method it opens the progressbar, but the bar never fills, it just keeps adding movies to the list and looking up the updated info without filling the bar. I thought by using it in a different threat would fix this problem?

有什么想法吗?

非常感谢

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

I tried using the BackgroundWorker. I added a background worker to my method and returned a ReportProgress back. I also added a backgroundWorker to my main class which uses the method, with eventhandlers. It didn't work however. I'm not very familiar with different threats. How should I go about then? I tried it like in here:

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

推荐答案

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

Since you are in the same thread as the UI, the progress bar does not refresh, in fact the whole window is not refreshed because your operation blocks the main UI thread.

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

Look at the BackgroundWorker Component to load your movie list, it contains everything you need.

这篇关于c#ProgressBar问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆