1 Replies - 1247 Views - Last Post: 09 April 2015 - 06:31 AM Rate Topic: -----

#1 chikku07   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 16-December 08

Multitasking on process and progress bar

Posted 09 April 2015 - 06:20 AM

Hi,

Am developing windows forms application where in my form am fetching data into list from some object which takes around 2 minutes to complete processing so meantime i want to show progress bar before starting process till it ends with percentage. I have return a code to do multitasking but it is throwing error as "Cross-thread operation not valid: Control 'listOrg' accessed from a thread other than the thread it was created on". Please help to resolve my issue and find my code.
private void btnOrg_Click(object sender, EventArgs e)
        {
            try
            {
                // To report progress from the background worker we need to set this property
                backgroundWorker1.WorkerReportsProgress = true;
                // This event will be raised on the worker thread when the worker starts
                backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                // This event will be raised when we call ReportProgress
                backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

                // Start the background worker
                backgroundWorker1.RunWorkerAsync();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please provide valid Server Name");
                ER.WriteErrorLog("The application terminated with an error at Login Form : GetOrgServiceProxy().");
                ER.WriteErrorLog(ex.Message);
            }
        }

 private void FetchOrganization()
        {
            if (txtServerName.Text != string.Empty)
            {
                if (_discoveryServiceAddress == null || _discoveryServiceAddress == string.Empty)
                {
                    _discoveryServiceAddress = "http://" + txtServerName.Text + "/XRMServices/2011/Discovery.svc";
                    
                    IServiceManagement<IDiscoveryService> serviceManagement = ServiceConfigurationFactory.CreateManagement<IDiscoveryService>(new Uri(_discoveryServiceAddress));
                    AuthenticationProviderType endpointType = serviceManagement.AuthenticationType;

                    // Set the credentials.
                    AuthenticationCredentials authCredentials = GetCredentials(serviceManagement, endpointType);

                    String organizationUri = String.Empty;
                    // Get the discovery service proxy.
                    using (DiscoveryServiceProxy discoveryProxy = GetProxy<IDiscoveryService, DiscoveryServiceProxy>(serviceManagement, authCredentials))
                    {

                        // Obtain organization information from the Discovery service. 
                        if (discoveryProxy != null)
                        {
                            // Obtain information about the organizations that the system user belongs to.
                            OrganizationDetailCollection orgs = DiscoverOrganizations(discoveryProxy);

                            if (orgs != null)
                            {
                                int i = 0;
                                //Add organization list
                                foreach (var item in orgs)
                                {
                                    listOrg.Items.Add(item.UniqueName);
                                    // Report progress to 'UI' thread
                                    [b]backgroundWorker1.ReportProgress(i+1);[/b]
                                }

                                if (listOrg.Items.Count > 0)
                                {
                                    listOrg.SelectedItem = listOrg.Items[0];
                                    _organizationUniqueName = Convert.ToString(listOrg.SelectedItem);
                                    // Obtains the Web address (Uri) of the target organization.
                                    organizationUri = FindOrganization(_organizationUniqueName, orgs.ToArray()).Endpoints[EndpointType.OrganizationService];
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Please provide valid Server Name");
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            FetchOrganization();
        }

        // Back on the 'UI' thread so we can update the progress bar
        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // The progress percentage is a property of e
            progressBar1.Value = e.ProgressPercentage;
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Multitasking on process and progress bar

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Multitasking on process and progress bar

Posted 09 April 2015 - 06:31 AM

See FAQ #18 - Multi-threading and cross-thread error

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated March 2015
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1