0

Here is a question very close to mine (I think): Running a method in BackGroundWorker and Showing ProgressBar

Here is my code, it's not freezing my Main form anymore but the function doesn't do its job.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using YoutubeSearch;


namespace searchyoutubeTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            myBGWorker.WorkerReportsProgress = true;
        }

        void myBGWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            myProgressBar.Value = e.ProgressPercentage;
        }

        void myBGWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            btnSearch.Enabled = true;
            MessageBox.Show("Done");


        }


        private void myBGWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            VideoSearch items = new VideoSearch();
            List<Video> list = new List<Video>();
            foreach (var item in items.SearchQuery(txtSearch.Text, 1))
            {
                Video video = new Video();
                video.Title = item.Title;
                video.Author = item.Author;
                video.Url = item.Url;
                byte[] imageBytes = new WebClient().DownloadData(item.Thumbnail);
                using (MemoryStream ms = new MemoryStream(imageBytes))
                {
                    video.Thumbnail = Image.FromStream(ms);
                }

                list.Add(video);

            }
            e.Result = list;
        }


        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Enabled = false;
            myBGWorker.RunWorkerAsync();

            //VideoSearch items = new VideoSearch();
            //List<Video> list = new List<Video>();
            //foreach (var item in items.SearchQuery(txtSearch.Text, 1))
            //{
            //    Video video = new Video();
            //    video.Title = item.Title;
            //    video.Author = item.Author;
            //    video.Url = item.Url;
            //    byte[] imageBytes = new WebClient().DownloadData(item.Thumbnail);
            //    using (MemoryStream ms = new MemoryStream(imageBytes))
            //    {
            //        video.Thumbnail = Image.FromStream(ms);
            //    }

            //    list.Add(video);

            //}
            //videoBindingSource.DataSource = list;
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(this, new EventArgs());
            }
        }


    }
}

But if I do only the function, it works:

 //VideoSearch items = new VideoSearch();
        //List<Video> list = new List<Video>();
        //foreach (var item in items.SearchQuery(txtSearch.Text, 1))
        //{
        //    Video video = new Video();
        //    video.Title = item.Title;
        //    video.Author = item.Author;
        //    video.Url = item.Url;
        //    byte[] imageBytes = new WebClient().DownloadData(item.Thumbnail);
        //    using (MemoryStream ms = new MemoryStream(imageBytes))
        //    {
        //        video.Thumbnail = Image.FromStream(ms);
        //    }

        //    list.Add(video);

        //}
        //videoBindingSource.DataSource = list;

Any idea what I am doing wrong?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
HitsmanFb
  • 3
  • 4

1 Answers1

0

I assume you correctly added the events for the button and onload of the form.
You didnt attach the events for the BackgroundWorker. When you click on the searchbutton, the workers starts and the DoWork method is invoked on a seperate thread.

public Form1()
{
    InitializeComponent();

    myBGWorker.WorkerReportsProgress = true;
    myBGWorker.DoWork += myBGWorker_DoWork;
    myBGWorker.ProgressChanged += myBGWorker_ProgressChanged;
    myBGWorker.RunWorkerCompleted += myBGWorker_RunWorkerCompleted;
}
Sievajet
  • 3,443
  • 2
  • 18
  • 22
  • This has solved the problem, Thanks @Sievajet but the DataGridView is not being populated. It just shows the "Done" message at the end. I know this should be another problem, but if you could just help me with this last one, appreciate it. I think the problem is the part `e.Result = list;` because the code in the normal(without the myBGWorker) is `videoBindingSource.DataSource = list;` . How do I go about the return of the `DoWork`. And oh! the myProgressBar doesn't animate marquee. Thanks in advanced everyone. – HitsmanFb Jan 10 '18 at 09:50
  • In the `DoWork` you only set the e.Result with the result from your process. In the `RunWorkerCompleted` event you should do something like `videoBindingSource.DataSource = e.Result` To fill the grid. – Sievajet Jan 10 '18 at 10:05
  • Perfect! Thank you so much @Sievajet. I did like this `list.Add(video);` `e.Result = list;` About the myProgressBar, I added these to DoWork `myProgressBar.Value = myProgressBar.Minimum;` `int progressCounter = 0;` and inside foreach `progressCounter = progressCounter + 1;` `myBGWorker.ReportProgress(progressCounter);` but the progressbar finishes only on 1/3. Sorry, if it's not allowed to ask 3 questions at a time, it's ok if you don't answer. Thanks – HitsmanFb Jan 10 '18 at 11:54
  • Check you minimum and maximum value. and check whether you increment correctly. at the completion of your process the progress should have the maximum value. – Sievajet Jan 10 '18 at 12:12
  • Hi @Sievajet, Thank you for your time and help. I've put these on the RunWorkerCompleted: `myProgressBar.Value = myProgressBar.Maximum;` `myProgressBar.Size = myProgressBar.MaximumSize;` but still not reflecting. :) – HitsmanFb Jan 10 '18 at 12:22
  • Hi @Sievajet, you've lost me there. I didn't find any `myProgressBar.MaximumValue`. Sorry for being noob :) – HitsmanFb Jan 10 '18 at 12:59
  • `myProgressBar.Maximum` – Sievajet Jan 10 '18 at 13:02