WPF and Threading
-
Hello everyone, I have the following peace of code in a WPF application.
System.Threading.ThreadStart threadstartdelegate = delegate()
{
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(1000);
OnlineClientListBoxItem item = new OnlineClientListBoxItem();
item.SetUserDetails(Name: "Client " + i.ToString(), Userid: i);lstbOnlineClients.Dispatcher.Invoke(new Action<OnlineClientListBoxItem>((x) => { lstbOnlineClients.Items.Add(x); }), System.Windows.Threading.DispatcherPriority.Normal, item); } }; System.Threading.Thread mainThread = new System.Threading.Thread(threadstartdelegate); mainThread.SetApartmentState(System.Threading.ApartmentState.STA); mainThread.Start();
and at this part lstbOnlineClients.Items.Add(x); it is giving me the following error "The calling thread cannot access this object because a different thread owns it." can anyone help me because this is the method in which you can invoce a control's method from another thread. Any ideas? Thank you in advance
-
Hello everyone, I have the following peace of code in a WPF application.
System.Threading.ThreadStart threadstartdelegate = delegate()
{
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(1000);
OnlineClientListBoxItem item = new OnlineClientListBoxItem();
item.SetUserDetails(Name: "Client " + i.ToString(), Userid: i);lstbOnlineClients.Dispatcher.Invoke(new Action<OnlineClientListBoxItem>((x) => { lstbOnlineClients.Items.Add(x); }), System.Windows.Threading.DispatcherPriority.Normal, item); } }; System.Threading.Thread mainThread = new System.Threading.Thread(threadstartdelegate); mainThread.SetApartmentState(System.Threading.ApartmentState.STA); mainThread.Start();
and at this part lstbOnlineClients.Items.Add(x); it is giving me the following error "The calling thread cannot access this object because a different thread owns it." can anyone help me because this is the method in which you can invoce a control's method from another thread. Any ideas? Thank you in advance
This is the most general issue. You can access UI objects only from UI thread. In WPF the UI thread is pointed using Dispatcher object. So you can replace the code
lstbOnlineClients.Items.Add(x);
with
this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Forms.MethodInvoker(delegate() { lstbOnlineClients.Items.Add(x); }));
Dispatcher will invoke the code in the UI thread. I hope this will help you. :rose:Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
Visit My Website-->**
-
This is the most general issue. You can access UI objects only from UI thread. In WPF the UI thread is pointed using Dispatcher object. So you can replace the code
lstbOnlineClients.Items.Add(x);
with
this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Forms.MethodInvoker(delegate() { lstbOnlineClients.Items.Add(x); }));
Dispatcher will invoke the code in the UI thread. I hope this will help you. :rose:Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
Visit My Website-->**
The OP already uses Dispather.Invoke.