我的代码如下
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) </pamam>
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);
}
}
获取网格数据中的步骤objUDMCountryStandards.Country = txtSearchCountry.Text.Trim() != string.Empty ? txtSearchCountry.Text : null;
抛出异常
调用线程无法访问此对象,因为另一个线程拥有它。
这有什么不对?
这是人们入门的常见问题。每当您从主线程以外的线程更新UI元素时,您需要使用:
this.Dispatcher.Invoke(() =>
{
...// your code here.
});
您还可以使用control.Dispatcher.CheckAccess()
来检查当前线程是否拥有该控件。如果它拥有它,您的代码看起来很正常。否则,请使用上述模式。
如上所述,here,Dispatcher.Invoke
可以冻结用户界面。应该使用Dispatcher.BeginInvoke
代替。
这是一个方便的扩展类,用于简化检查和调用调度程序调用。
示例用法:(从WPF窗口调用)
this Dispatcher.InvokeIfRequired(new Action(() =>
{
logTextbox.AppendText(message);
logTextbox.ScrollToEnd();
}));
扩展类:
using System;
using System.Windows.Threading;
namespace WpfUtility
{
public static class DispatcherExtension
{
public static void InvokeIfRequired(this Dispatcher dispatcher, Action action)
{
if (dispatcher == null)
{
return;
}
if (!dispatcher.CheckAccess())
{
dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle);
return;
}
action();
}
}
}
这适合我。
new Thread(() =>
{
Thread.CurrentThread.IsBackground = false;
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate {
//Your Code here.
}, null);
}).Start();
此外,另一种解决方案是确保您的控件是在UI线程中创建的,而不是由后台工作线程创建的。
当我将级联组合框添加到我的WPF应用程序时,我一直收到错误,并使用此API解决了错误:
using System.Windows.Data;
private readonly object _lock = new object();
private CustomObservableCollection<string> _myUiBoundProperty;
public CustomObservableCollection<string> MyUiBoundProperty
{
get { return _myUiBoundProperty; }
set
{
if (value == _myUiBoundProperty) return;
_myUiBoundProperty = value;
NotifyPropertyChanged(nameof(MyUiBoundProperty));
}
}
public MyViewModelCtor(INavigationService navigationService)
{
// Other code...
BindingOperations.EnableCollectionSynchronization(AvailableDefectSubCategories, _lock );
}
Dispatcher.Invoke
的另一个好用途是在执行其他任务的函数中立即更新UI:
// Force WPF to render UI changes immediately with this magic line of code...
Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle);
我使用它来更新按钮文本为“Processing ...”并在制作WebClient
请求时禁用它。
要加上我的2美分,即使您通过System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke()
调用代码,也会发生异常。
关键是你必须调用你试图访问的控件的Invoke()
的Dispatcher
,在某些情况下可能与System.Windows.Threading.Dispatcher.CurrentDispatcher
不同。因此,你应该使用YourControl.Dispatcher.Invoke()
是安全的。在我意识到这一点之前,我正在敲打我的头几个小时。
对于未来的读者来说,在较新版本的.NET(4.0及更高版本)中看起来已经发生了变化。现在,在更新VM中的UI支持属性时,您不再需要担心正确的调度程序。 WPF引擎将在正确的UI线程上封送跨线程调用。查看更多详情here。感谢@aaronburro的信息和链接。您可能还想在评论中阅读下面的对话。
如果遇到此问题并且在WPF中使用BitmapSource
或ImageSource
时在单独的工作线程上创建UI控件,请先将Freeze()
方法作为参数传递给BitmapSource
方法,然后再将ImageSource
方法作为参数传递给任何方法。使用Application.Current.Dispatcher.Invoke()
在这种情况下不起作用
这发生在我身上,因为我试图在access UI
中使用another thread insted of UI thread
组件
像这样
private void button_Click(object sender, RoutedEventArgs e)
{
new Thread(SyncProcces).Start();
}
private void SyncProcces()
{
string val1 = null, val2 = null;
//here is the problem
val1 = textBox1.Text;//access UI in another thread
val2 = textBox2.Text;//access UI in another thread
localStore = new LocalStore(val1);
remoteStore = new RemoteStore(val2);
}
要解决这个问题,请在what Candide mentioned above in his answer中包装任何ui调用
private void SyncProcces()
{
string val1 = null, val2 = null;
this.Dispatcher.Invoke((Action)(() =>
{//this refer to form in WPF application
val1 = textBox.Text;
val2 = textBox_Copy.Text;
}));
localStore = new LocalStore(val1);
remoteStore = new RemoteStore(val2 );
}
由于某种原因,Candide的答案没有建立。不过,这很有帮助,因为它让我找到了这个,它完美地工作:
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((Action)(() =>
{
//your code here...
}));
您需要更新到UI,所以使用
Dispatcher.BeginInvoke(new Action(() => {GetGridData(null, 0)}));
我还发现System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke()
并不总是目标控制的调度员,正如dotNet在他的回答中写道的那样。我没有访问控制自己的调度程序,所以我使用Application.Current.Dispatcher
,它解决了问题。
问题是你从后台线程调用GetGridData
。此方法访问绑定到主线程的几个WPF控件。任何从后台线程访问它们的尝试都将导致此错误。
为了回到正确的线程,你应该使用SynchronizationContext.Current.Post
。但是在这种特殊情况下,您所做的大部分工作似乎都是基于UI的。因此,您将创建一个后台线程,以便立即返回到UI线程并完成一些工作。您需要稍微重构一下代码,以便它可以在后台线程上执行昂贵的工作,然后将新数据发布到UI线程