none
progressbar and backgroundworker not working RRS feed

  • Frage

  • hi,

    i am trying to revoke update a progressbar in a sync-dialog from a synchronization class  but it won't work, here is my nippet: a the bottom in bold the counter.

    public partial class SettingsDlg : Form
        {
            GetCRMContacts getContact = new GetCRMContacts();
            Ribbon1 rib1 = new Ribbon1();
            
    
            public void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) {
                //e.Argument always contains whatever was sent to the background worker 
                // in RunWorkerAsync. We can simply cast it to its original type.
    
                DataSet ds = (DataSet)e.Argument; 
                var bgw = (BackgroundWorker)sender;
    
                var evthandler = new ProgressChangedEventHandler((o,a) => bgw.ReportProgress(a.ProgressPercentage));
                getContact.ProgressChanged += evthandler;
                this.getContact.ProcessData(ds);
                getContact.ProgressChanged -= evthandler; //necessary to stop listening
        }
    
            private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                this.progressBar1.Minimum = 0;
                this.progressBar1.Maximum = getContact.favcount;
                this.progressBar1.Value = e.ProgressPercentage;
            }

    class GetCRMContacts
        {
            bool contactfound = false;
            public int icountFAV;
            public int favcount = 0;
    
            Microsoft.Office.Interop.Outlook.Items OutlookItems;
            //Microsoft.Office.Interop.Outlook.Application outlookObj;
            //MAPIFolder Folder_Contacts;
    
            // create new instance of the Outlook app
            static Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
            // get path to the MAPI api
            Microsoft.Office.Interop.Outlook.NameSpace ns = app.GetNamespace("MAPI");
            // get path to the contacts folder.  Use the OlDefaultFolders enum to specify
            // that we want to get the olFolderContacts folder.
            Microsoft.Office.Interop.Outlook.MAPIFolder contactsFolder = app.ActiveExplorer().Session.GetDefaultFolder(
                Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
    
            public event ProgressChangedEventHandler ProgressChanged;
            protected virtual void OnProgressChanged(ProgressChangedEventArgs e)
            {
                var hand = ProgressChanged;
                if (hand != null) hand(this, e);
            }
    
            public void ProcessData(DataSet ds)
            {
                //for (int i = 1; i <= 10; i++)
                //{
                    // Perform a time consuming operation and report progress.
                    System.Threading.Thread.Sleep(500);
    
                    var e = new ProgressChangedEventArgs(icountFAV, null);
                //}
            }
            
            public bool GetFavContacts()
            {
                SettingsDlg setDlg = new SettingsDlg();
     
    
                FAVcontactsDataContext infoman_fav_contact = new FAVcontactsDataContext();
    
                string logged_on_username;
                
                int i = 0;
                
    
              
                DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName);
               
    
                logged_on_username = de.Properties["fullName"].Value.ToString();
    
                var favcontacts = from c in infoman_fav_contact.Infoman_Favorit_Kontakts
                                  where c.CreatedByName == logged_on_username
                                  select c;
    
                favcount = favcontacts.Count();
                
                //setDlg.SetProgressBarMax(favcount);
                //setDlg.SetProgressBarMin(0);
                //setDlg.SetProgressBarStep(1);
                          
    
                foreach(var fav in favcontacts)
                {
                    icountFAV++;
                    
                    MessageBox.Show(fav.infoman_favoritkontaktidName + " " + fav.Infoman_Favorit_KontaktId + " " + fav.infoman_favoritkontaktid);
    
                    
                    //setDlg.SetProgressBarValue(i);
                    var e = new ProgressChangedEventArgs(icountFAV, null);
    thank you in advance for your help


    Andreas

    Montag, 23. September 2013 09:39

Antworten

  • Hallo,
    im Deutschen Forum kann man auch Deutsch fragen ;)

    So wie es mir scheint, hast du die Funktionsweise von Events noch nicht ganz verstanden bzw. hier falsch angewendet. In der Methode ProcessData erzeugst du zwar die neuen EventArgs, aber du löst das Event nicht aus.

    OnProgressChanged (e);

    Weiterhin bin ich mir mit folgenden Zeilen nicht ganz sicher:

                this.progressBar1.Minimum = 0;
                this.progressBar1.Maximum = getContact.favcount;
                this.progressBar1.Value = e.ProgressPercentage;

    e.ProgressPercentage stellt (normalerweise) beim BackgroundWorker einen Wert zwischen einschließlich 0 und einschließlich 100 dar. Das Maximum sollte außerdem schon vor der ersten Prozessänderung bekannt sein, da der Fortschritt sich sonst "unkontrolliert" bewegen könnte.

    Nun wird das Feld icountFAV nur in GetFavContacts erhöht. Das hat zur Folge, das sich hier auch nichts ändern wird. Außerdem fehlt dem Feld der Standartwert (Bei Deklaration zuweisen)

    Wenn du nun die for-Schleife wieder auskommentierst und i anstelle von icountFAV übergibst und dann noch das Event auslöst, sollte es funktionieren. Vorrausgesetzt, die Ausgabe (backgroundworker2_ProgressChanged) arbeitet richtig.


    Koopakiller [kuːpakɪllɐ] (Tom Lambert)
    Webseite | Code Beispiele | Facebook | Twitter | Snippets   C# ↔ VB.NET Konverter
    Markiert bitte beantwortende Posts als Antwort und bewertet Beiträge. Danke.

    Montag, 23. September 2013 13:11
    Moderator

Alle Antworten