0
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;
using Ionic.Zip;
namespace downloader
{
public partial class GUI : Form
{
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public GUI()
{
InitializeComponent();
}
private void Download_Click(object sender, EventArgs e)
{
label1.Text = ("Downloading...");
WebClient x = new WebClient();
x.DownloadProgressChanged += new DownloadProgressChangedEventHandler(x_DownloadProgressChanged);
x.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(x_DownloadFileCompleted);
x.DownloadFileAsync(new Uri("http://google.com/"), desktop + "\\index.html");
download.Enabled = false;
}
void x_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
label2.Text = null;
label1.Text = "Download Complete.";
MessageBox.Show("Download Done.", "Done!");
}
public void x_DownloadProgressChanged(Object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
this.Text = ":: Kyle :: " + e.ProgressPercentage + "%";
label2.Text = e.BytesReceived + " bytes saved.";
}
public void unzip(String zFile)
{
Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(zFile);
zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(zip_ExtractProgress);
zip.ExtractAll(desktop, ExtractExistingFileAction.OverwriteSilently);
zip.Dispose();
zip = null;
}
public void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
{
if (e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten)
{
this.label2.Text = e.BytesTransferred.ToString(); //unsafe also?
}
else if (e.EventType == ZipProgressEventType.Extracting_BeforeExtractEntry)
{
this.label3.Text = e.CurrentEntry.FileName; //unsafe
}
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
unzip(desktop + "\\Client.zip");
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
MessageBox.Show("Done Unzipping.");
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
}
}
テキストラベルを修正するにはどうすればよいですか?私はbackgroundWorkerを使用していますが、ラベルなしで動作しますが、私はemを持っていると言い続けます。Cross-thread operation not valid: Control 'label3' accessed from a thread other than the thread it was created on.
スレッドセーフな呼び出しをbackgroundWorkerから固定
メインUIのパフォーマンスを向上させるには、どの方法をお勧めしますか? – Kyle
backgroundWorkerプログレスバーがUIを更新していないことに気づきました。私もそれを修正するために何ができるか知っていますか? – Kyle
両者は同じように動作します。 – SLaks