修复来自backgroundWorker的线程安全调用

发布于 2024-10-11 02:00:38 字数 3257 浏览 8 评论 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,它可以在没有标签的情况下工作,但是当我有它们时,它一直说跨线程操作无效:控制'label3'从创建它的线程以外的线程访问。

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;
        }
    }
}

How do I fix my text labels? I'm using a backgroundWorker and it works without the labels but when I have em it keeps saying Cross-thread operation not valid: Control 'label3' accessed from a thread other than the thread it was created on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2024-10-18 02:00:38

您应该通过调用BackgroundWorker 的ReportProgress 方法来报告进度。

或者,您可以通过调用 BeginInvoke 在 UI 线程上运行。

You should report the progress by calling the BackgroundWorker's ReportProgress method.

Alternatively, you can run on the UI thread by calling BeginInvoke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文