SORU
16 Mart 2012, Cuma


Arama iş parçacığı farklı bir iş sahibi için erişim bu nesne olamaz

Benim kod aşağıdaki gibidir

public CountryStandards()
    {
        InitializeComponent();
        try
        {
            FillPageControls();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Country Standards", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }

    /// <summary>
    /// Fills the page controls.
    /// </summary>
    private void FillPageControls()
    {
        popUpProgressBar.IsOpen = true;
        lblProgress.Content = "Loading. Please wait...";
        progress.IsIndeterminate = true;
        worker = new BackgroundWorker();
        worker.DoWork  = new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
        worker.ProgressChanged  = new System.ComponentModel.ProgressChangedEventHandler(worker_ProgressChanged);
        worker.WorkerReportsProgress = true;
        worker.WorkerSupportsCancellation = true;
        worker.RunWorkerCompleted  = new System.ComponentModel.RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        worker.RunWorkerAsync();                    
    }


    private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        GetGridData(null, 0); // filling grid
    }

    private void worker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
    {
        progress.Value = e.ProgressPercentage;
    }


    private void worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    {
        worker = null;
        popUpProgressBar.IsOpen = false;

        //filling Region dropdown
        Standards.UDMCountryStandards objUDMCountryStandards = new Standards.UDMCountryStandards();
        objUDMCountryStandards.Operation = "SELECT_REGION";
        DataSet dsRegionStandards = objStandardsBusinessLayer.GetCountryStandards(objUDMCountryStandards);
        if (!StandardsDefault.IsNullOrEmptyDataTable(dsRegionStandards, 0))
            StandardsDefault.FillComboBox(cmbRegion, dsRegionStandards.Tables[0], "Region", "RegionId");

        //filling Currency dropdown
        objUDMCountryStandards = new Standards.UDMCountryStandards();
        objUDMCountryStandards.Operation = "SELECT_CURRENCY";
        DataSet dsCurrencyStandards = objStandardsBusinessLayer.GetCountryStandards(objUDMCountryStandards);
        if (!StandardsDefault.IsNullOrEmptyDataTable(dsCurrencyStandards, 0))
            StandardsDefault.FillComboBox(cmbCurrency, dsCurrencyStandards.Tables[0], "CurrencyName", "CurrencyId");

        if (Users.UserRole != "Admin")
            btnSave.IsEnabled = false;

    }

    /// <summary>
    /// Gets the grid data.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="pageIndex">Index of the page.( used in case of paging)</param>
    private void GetGridData(object sender, int pageIndex)
    {
        Standards.UDMCountryStandards objUDMCountryStandards = new Standards.UDMCountryStandards();
        objUDMCountryStandards.Operation = "SELECT";
        objUDMCountryStandards.Country = txtSearchCountry.Text.Trim() != string.Empty ? txtSearchCountry.Text : null;
        DataSet dsCountryStandards = objStandardsBusinessLayer.GetCountryStandards(objUDMCountryStandards);
        if (!StandardsDefault.IsNullOrEmptyDataTable(dsCountryStandards, 0) && (chkbxMarketsSearch.IsChecked == true || chkbxBudgetsSearch.IsChecked == true || chkbxProgramsSearch.IsChecked == true))
        {
            DataTable objDataTable = StandardsDefault.FilterDatatableForModules(dsCountryStandards.Tables[0], "Country", chkbxMarketsSearch, chkbxBudgetsSearch, chkbxProgramsSearch);
            dgCountryList.ItemsSource = objDataTable.DefaultView;
        }
        else
        {
            MessageBox.Show("No Records Found", "Country Standards", MessageBoxButton.OK, MessageBoxImage.Information);
            btnClear_Click(null, null);
        }
    }

Al veri ızgara objUDMCountryStandards.Country = txtSearchCountry.Text.Trim() != string.Empty ? txtSearchCountry.Text : null; istisna atar "farklı bir iş sahibi çünkü iplik erişemiyor bu çağrılması." adım . yanlış olan nedir burada?

CEVAP
16 Mart 2012, Cuma


Bu insanlar başlarken ile ortak bir sorundur. Bir iş parçacığı ana iş parçacığı dışındaki UI öğeleri güncelleştirmek her zaman, her zaman kullanmak gerekir:

this.Dispatcher.Invoke((Action)(() =>
    {
        ...// your code here.
    }));

Ayrıca control.Dispatcher.CheckAccess() geçerli iş parçacığı kontrol sahibi olup olmadığını kontrol etmek için kullanabilirsiniz. Eğer kendi yaparsa, kodunuzu normal gibi görünüyor. Aksi takdirde, yukarıda desen kullanın.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Eric Anthony

    Eric Anthony

    13 AĞUSTOS 2011
  • fast2hell

    fast2hell

    16 AĞUSTOS 2006
  • Fraser Raft

    Fraser Raft

    9 Mart 2010