0

YouTube から 200 個のビデオ URL を取得しています。しかし、アプリがフリーズします。どうすれば機能するのでしょうか。バックグラウンド ワーカーでも使用していますが、まだ機能していません。

これでバックグラウンド ワーカーを使用しました。listBox2 にビデオ タイトルを追加したいと考えています。

初期化されたワーカー ..

    worker1 = new BackgroundWorker();

    worker1.DoWork += new DoWorkEventHandler(worker1_DoWork);
    worker1.ProgressChanged += new ProgressChangedEventHandler(worker1_ProgressChanged);
    worker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker1_RunWorkerCompleted);

    worker1.WorkerReportsProgress = true;   
worker1.WorkerSupportsCancellation = true;

    int ii = 0;
    int jj = 0;

作業機能を実行します。

void worker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int progressVal = 0;
        textBox1.Invoke(new Action(
                            delegate()
                            {

                                if (worker1.CancellationPending)
                                { return; }

                                    string[] videoLink = textBox1.Text.Split(new char[] { '\n' });
                                    progressBar1.Maximum = videoLink.Length ;

                                    foreach (string s in videoLink)
                                    {
                                        string videoID;

                                        try
                                        {
                                            videoID = s.Split(new char[] { '=' })[1].Trim();
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show(this, "Please enter correct video links\nExample : http://www.youtube.com/watch?v=123456789", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            return;
                                        }

                                        YouTubeRequestSettings reqSettings = new YouTubeRequestSettings("Youtube Comments", devkey, usernames[jj], password) { Timeout = -1 };
                                        YouTubeRequest request = new YouTubeRequest(reqSettings);

                                        jj++;
                                        if (jj == usernames.Length) jj = 0;

                                        string commentContent = listBox1.Items[ii].ToString();
                                        ii++;
                                        if (ii == listBox1.Items.Count) ii = 0;

                                        Comment comment = new Comment();
                                        comment.Content = commentContent;

                                        try
                                        {
                                            Video video = request.Retrieve<Video>(new Uri("http://gdata.youtube.com/feeds/api/videos/" + videoID));
                                            worker1.ReportProgress(progressVal++, "[" + comment.Content + "] Added to : " + "VideoTitle");

                                            //    request.AddComment(request.Retrieve<Video>(new Uri("http://gdata.youtube.com/feeds/api/videos/" + videoID)), comment);
                                        }
                                        catch (Exception ex)
                                        {
                                            worker1.ReportProgress(progressVal++, "Error Accoured : " + "VideoTitle");
                                        }
                                    }


                            }));


    }

リストボックスにタイトルを追加するたびに進行するようにしたいのですが、進行状況は最後にしか表示されません。プロセス全体を完了した後。

void worker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progressBar1.Value = progressBar1.Maximum;
        this.Show();
    }

    void worker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        listBox2.Items.Add(e.UserState.ToString());

    }
4

1 に答える 1

0

リストボックスの更新コマンドを進行状況の変更に追加します。

void worker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    listBox2.Items.Add(e.UserState.ToString());
    listbox2.Items.Refresh();
}

バインディングにINotifyPropertyChangedを実装することもできます (WPF を実行している場合)。

于 2013-09-02T23:27:41.987 に答える