0

我实际上有以下代码:

private Stopwatch _sw;

public void DownloadFile(string url, string fileName)
{
    string path = @"C:\DL\";

    Thread bgThread = new Thread(() =>
    {

        _sw = new Stopwatch();
        _sw.Start();
        labelDownloadAudioStatusText.Visibility = Visibility.Visible;
        using (WebClient webClient = new WebClient())
        {
            webClient.DownloadFileCompleted +=
                new AsyncCompletedEventHandler(DownloadCompleted);
            webClient.DownloadProgressChanged +=
                new DownloadProgressChangedEventHandler(DownloadStatusChanged);
            webClient.DownloadFileAsync(new Uri(url), path + fileName);
        }
    });

    bgThread.Start();
}

void DownloadStatusChanged(object sender, DownloadProgressChangedEventArgs e)
{
    this.BeginInvoke((MethodInvoker) delegate
    {
        int percent = 0;
        if (e.ProgressPercentage != percent)
        {
            percent = e.ProgressPercentage;
            progressBarDownloadAudio.Value = percent;

            labelDownloadAudioProgress.Content = percent + "%";
            labelDownloadAudioDlRate.Content =
                (Convert.ToDouble(e.BytesReceived)/1024/
                _sw.Elapsed.TotalSeconds).ToString("0.00") + " kb/s";

            Thread.Sleep(50);
        }
    });
}

private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
    this.BeginInvoke((MethodInvoker) delegate
    {

        labelDownloadAudioDlRate.Content = "0 kb/s";
        labelDownloadAudioStatusText.Visibility = Visibility.Hidden;
    });
}

我的问题是,在没有外线程的先前版本中,整个 GUI 偶尔冻结,下载完成后 GUI 是流动的。所以我搜索了一下,发现了这个:https ://stackoverflow.com/a/9459441/2288470

答案是将所有内容打包到一个单独的线程中,该线程执行与 的交互,但我得到了错误,找不到DownloadFileAsync该方法。BeginInvoke

4

2 回答 2

1

使用 WPF 时,类BeginInvoke不公开该方法Window,就像Form在 WinForms 中一样。相反,您应该使用Dispatcher.BeginInvoke.


工作代码:

private Stopwatch _sw;

public void DownloadFile(string url, string fileName)
{
    string path = @"C:\DL\";

    Thread bgThread = new Thread(() =>
    {
        _sw = new Stopwatch();
        _sw.Start();
        labelDownloadAudioStatusText.Visibility = Visibility.Visible;
        using (WebClient webClient = new WebClient())
        {
            webClient.DownloadFileCompleted +=
                new AsyncCompletedEventHandler(DownloadCompleted);
            webClient.DownloadProgressChanged +=
                new DownloadProgressChangedEventHandler(DownloadStatusChanged);
            webClient.DownloadFileAsync(new Uri(url), path + fileName);
        }
    });

    bgThread.Start();
}

void DownloadStatusChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Dispatcher.BeginInvoke((MethodInvoker) delegate
    {
        int percent = 0;

        if (e.ProgressPercentage != percent)
        {
            percent = e.ProgressPercentage;
            progressBarDownloadAudio.Value = percent;

            labelDownloadAudioProgress.Content = percent + "%";
            labelDownloadAudioDlRate.Content =
                (Convert.ToDouble(e.BytesReceived)/1024/
                _sw.Elapsed.TotalSeconds).ToString("0.00") + " kb/s";

            Thread.Sleep(50);
        }
    });
}

private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
    Dispatcher.BeginInvoke((MethodInvoker) delegate
    {
        labelDownloadAudioDlRate.Content = "0 kb/s";
        labelDownloadAudioStatusText.Visibility = Visibility.Hidden;
    });
}
于 2013-04-17T22:33:51.870 回答
0

Invoke 方法和 BeginInvoke 方法在 System.Windows.Forms.Control 类上实现。如果您不是在 Form Class 中编写代码,则不能使用此方法。要解决此问题,请从 System.Windows.Forms.Control 类继承您的作业类,然后您可以使用 BeginInvoke 方法。请注意,您必须在主线程上创建实例。

public class JobClass : System.Windows.Forms.Control {
.....
}
于 2013-04-17T07:59:50.520 回答