none
בעייה ביצירת THREAD חדש RRS feed

  • שאלה

  • יצרתי THREAD חדש ואני מקבל את ההודעה הזאת:

    Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on.

    מישהו יודע מה הבעייה? כשאני מוריד את ה-THREAD זה עובד מצויין.

    הקוד שלי

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Ionic.Zip;
    using System.IO;
    using System.Threading;
    
    
    namespace MultiDrag
    {
        public partial class ExtractZIPFile : Form
        {
            public string zipFileName;
    
            public ExtractZIPFile()
            {
                InitializeComponent();
            }
    
            private void ExtractZIPFile_Load(object sender, EventArgs e)
            {
    
                Thread thread = new Thread(new ThreadStart(ExtractAllFiles));
                thread.Start();
            }
    
            public void ExtractAllFiles()
            {
                using (ZipFile zip = ZipFile.Read(zipFileName))
                {
    
                    Progress(zip.Count()); // קובע את הטווח של מד ההתקדמות לפי מספר הקבצים בקובץ הדחוס
                    
                    int i = 0;
    
                    try
                    {
                        foreach (ZipEntry ezip in zip)
                        {
                            i = i + 1;
                            progressBar1.Value = i;
                            this.Refresh();
                            ezip.Password = "12345678";
                            string getDirectoryFromString = zipFileName.Replace(".zip", "");
                            ezip.Extract(getDirectoryFromString, ExtractExistingFileAction.OverwriteSilently);
                        }
    
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Error");
                    }
                }
    
                this.Close();
            }
    
            public void Progress(int numberForProgressBar)
            {
                progressBar1.Maximum = numberForProgressBar;
                
            }
        }
    }

    • נערך על-ידי MaorD יום שני 30 יולי 2012 07:35
    יום שני 30 יולי 2012 07:17

תשובות

  • לא ניתן לעדכן פקדי ui מתוך thread שאינו זה שבו נוצרו הפקדים - כלל מוכר של סביבות חלונאיות כגון winforms ו-wpf.

    אנא קרא ב-MSDN על שימוש במחלקה שנקראת BackgroundWorker בשביל לטפל בתהליכים שכאלו שצריכים לדווח ל-UI על התקדמות.


    Please mark posts as answers/helpful if it answers your question.
    Senior Consultant on WCF, ASP.NET, Siverlight, and Entity Framework. Author of Microsoft's Official WCF 4 Course. Co-author of the Microsoft HPC/Azure burst whitepaper.
    Visit my blog: http://blogs.microsoft.co.il/blogs/idof

    • סומן כתשובה על-ידי Eran Sharvit יום שלישי 31 יולי 2012 14:23
    יום שני 30 יולי 2012 19:40
    מנחה דיון

כל התגובות

  • http://stackoverflow.com/questions/5789926/update-a-progressbar-from-another-thread
    יום שני 30 יולי 2012 09:31
  • לא ניתן לעדכן פקדי ui מתוך thread שאינו זה שבו נוצרו הפקדים - כלל מוכר של סביבות חלונאיות כגון winforms ו-wpf.

    אנא קרא ב-MSDN על שימוש במחלקה שנקראת BackgroundWorker בשביל לטפל בתהליכים שכאלו שצריכים לדווח ל-UI על התקדמות.


    Please mark posts as answers/helpful if it answers your question.
    Senior Consultant on WCF, ASP.NET, Siverlight, and Entity Framework. Author of Microsoft's Official WCF 4 Course. Co-author of the Microsoft HPC/Azure burst whitepaper.
    Visit my blog: http://blogs.microsoft.co.il/blogs/idof

    • סומן כתשובה על-ידי Eran Sharvit יום שלישי 31 יולי 2012 14:23
    יום שני 30 יולי 2012 19:40
    מנחה דיון
  • הנה פתרון יותר פשוט שאני משתמש בו (בד"כ לטקסט בוקס), ע"י שימוש בדלגייט אחד קטן

    http://bresleveloper.blogspot.co.il/2012/04/winforms-bypass-cross-thread-update.html

    יום שלישי 31 יולי 2012 06:35