Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day,
does embedding in ComboBox in ProWindows work the same as in Windows Form? Because I try to store values ​​(string) in the ComboBox menu but it always throws me a message: System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it.'

in the section for adding:
comboBox.Items.Add(name);


will be happy for any advice or help.

Thank you

David

What I have tried:

C#
private async void Button_Click(object sender, RoutedEventArgs e)
{
   await QueuedTask.Run(() =>
   {
	   var mapView = MapView.Active;
	   var featureNames = new List<string>();
	   var featureLayers = mapView.Map.GetLayersAsFlattenedList()
		   .OfType<BasicFeatureLayer>().ToList();
	   var featureLayersWithSel = featureLayers
		   .Where(fl => fl.GetSelection().GetCount() > 0).ToList();
	   foreach (var featureLayerWithSel in featureLayersWithSel)
	   {
		   var featureName = featureLayerWithSel.Name;
		   featureNames.Add(featureName);
	   }

	   foreach (var name in featureNames)
	   {
		   comboBox.Items.Add(name);
	   }
   });
}
Posted
Updated 1-Apr-22 2:00am
v4
Comments
TheRealSteveJudge 1-Apr-22 7:17am    
WPF or Windows Forms? What is "ProWindows"?
dejf111 1-Apr-22 7:21am    
It is an extension of ArcGIS for Visual Studio.
TheRealSteveJudge 1-Apr-22 7:34am    
OK. But what is it based on? Do you have xaml files in your solution?
TheRealSteveJudge 1-Apr-22 7:48am    
WPF is different from Windows Forms! See updated solution.

If ArcGIS is using WPF then you must change your code like this:
C#
Application.Current.Dispatcher.Invoke(() =>
{
	comboBox.Items.Add(name);
});
BTW: WPF is different from Windows Forms!
Please read this: Difference between WPF and WinForms - GeeksforGeeks[^]
 
Share this answer
 
v3
Comments
dejf111 1-Apr-22 7:50am    
Yes that's it, thank you !!!
TheRealSteveJudge 1-Apr-22 7:51am    
You're welcome!
Maciej Los 13-May-22 16:39pm    
5ed!
TheRealSteveJudge 16-May-22 4:47am    
Thank you!
It is already telling you the problem.

System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it.'

You are trying to access the combobox from a different thread that created it, the comboboxx was created by the UI thread and only the UI thread can manipulate the combobox.

You are trying to access it using inside the QueuedTask.Run which is a different thread hence the error message.
 
Share this answer
 
Comments
dejf111 1-Apr-22 7:17am    
So how do I fill in a ComboBox if I want to fill it in via a button?
Tony Hill 1-Apr-22 7:46am    
The easiest way is to perform the processing on the UI thread by removing the 'async' from the button event signature and the 'await QueuedTask.Run' and let the UI thread load the combobox.

Another way is to use Invoke to get the UI thread to perform the combobox loading.
dejf111 1-Apr-22 8:00am    
Thank you very much for the advice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900