背景工作者仍然冻结ui

时间:2019-05-10 14:53:35

标签: c# multithreading backgroundworker

即使我设置了后台工作人员,我似乎也无法使它不会冻结UI。

我在网上四处张望,但找不到解决方法。我已经尝试创建基本线程,并且可以正常工作,但是我还需要在运行时更新UI内容,这就是为什么我切换到背景工作人员的原因,但是如果您可以帮助我弄清楚如何使用基本线程,并且可以正常工作的话

using MediaToolkit;
using MediaToolkit.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using VideoLibrary;

namespace ConsoleApp1
{
public partial class Form1 : Form
{
    private List<VideoInfo> vids = new List<VideoInfo>();
    private List<VideoInfo> failedVids = new List<VideoInfo>();
    private int threads = 0;
    BackgroundWorker worker;
    private delegate void DELEGATE();
    static void Main(string[] args)
    {
        Form1 form = new Form1();
        form.ShowDialog();
    }

    public Form1()
    {
        InitializeComponent();
        worker = new BackgroundWorker();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if(threads == 0)
        {
            progressBar1.Maximum = vids.Count;
            progressBar1.Value = 0;
            failedVids.Clear();
            worker.DoWork += doWork;
            worker.RunWorkerAsync();
            //downloadVid();
        }else
        {
            Console.WriteLine("Waiting for threads" + threads);
        }
        /*Thread thread1 = new Thread(new ThreadStart(downloadVid));
        thread1.Start();*/
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        VideoInfo vid = new VideoInfo(tbLink.Text, tbTitle.Text, tbArtist.Text);
        vids.Add(vid);
        tbLink.Clear();
        tbTitle.Clear();
        listView1.Items.Add(vid.getLink());
    }

    private int downloadThread(int i)
    {
        Console.WriteLine(i);
        var source = @"C:\Users\derri\Downloads\DownloadTest\";
        var youtube = YouTube.Default;
        VideoInfo vidInfo = vids[(int) i];
        var vid = youtube.GetVideo(vidInfo.getLink());
        Console.WriteLine(vidInfo.getLink());
        try
        {
            File.WriteAllBytes(source + vid.FullName, vid.GetBytes());
        }
        catch (Exception e)
        {
            failedVids.Add(vids[(int)i]);
            Console.WriteLine(e);
            goto End;
        }

        var inputFile = new MediaFile { Filename = source + vid.FullName };
        var outputFile = new MediaFile { Filename = $"{source + vidInfo.getArtist() + " - " + vidInfo.getTitle()}.mp3" };

        using (var engine = new Engine())
        {
            engine.GetMetadata(inputFile);

            engine.Convert(inputFile, outputFile);
        }
        File.Exists(outputFile.Filename);
        File.Delete(inputFile.Filename);
        setTags(vidInfo.getArtist(), vidInfo.getTitle());
    End:
        threads--;
        return 1;
    }

    private void doWork(object sender, DoWorkEventArgs e)
    {
        Delegate del = new DELEGATE(downloadVid);
        this.Invoke(del);
    }

    private void downloadVid()
    {
        int prog = 0;
        for (int i = 0; i < vids.Count; i++)
        {
            Console.WriteLine(i);
            while ((threads > 5)) { }
            Thread thread = new Thread(() => { prog += downloadThread(i); });
            thread.Start();
            threads++;
            System.Threading.Thread.Sleep(1000);
            //thread.Join();
            /*ParameterizedThreadStart start = new ParameterizedThreadStart(downloadThread);
            Thread t = new Thread(start);
            t.Start(i);
            progressBar1.Value++;
            threads++;*/

        }

        while (threads > 0){}
        foreach (VideoInfo failedVid in failedVids)
        {
            listView2.Items.Add(failedVid.getLink());
        }
        listView1.Clear();
        vids.Clear();

    }

    private void setTags(string artist, string title)
    {
        TagLib.File file = TagLib.File.Create("C:\\Users\\derri\\Downloads\\DownloadTest\\" + artist + " - " + title + ".mp3");
        file.Tag.Artists = (new String[] { artist });
        file.Tag.Title = (title);
        file.Save();
    }
}

}

1 个答案:

答案 0 :(得分:0)

this.Invoke将在UI线程上运行。

研究如何使用BackgroundWorker类的ReportProgress或RunWorkerCompleted事件。两者都允许您将数据从后台线程传递到UI线程。