这是一个非常接近我的问题(我认为):Running a method in BackGroundWorker and Showing ProgressBar
这是我的代码,它不再冻结我的主表单,但功能不起作用。
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());
}
}
}
}
但如果我只做这个功能,那就有效:
//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;
知道我做错了吗?
答案 0 :(得分:0)
我假设您正确添加了按钮和表格上传的事件
你没有附上BackgroundWorker
的事件。当您单击搜索按钮时,工作程序将启动,并在单独的线程上调用DoWork
方法。
public Form1()
{
InitializeComponent();
myBGWorker.WorkerReportsProgress = true;
myBGWorker.DoWork += myBGWorker_DoWork;
myBGWorker.ProgressChanged += myBGWorker_ProgressChanged;
myBGWorker.RunWorkerCompleted += myBGWorker_RunWorkerCompleted;
}