调用线程无法访问此对象,因为其他线程拥有它.例外 [英] The calling Thread cannot access this object because a different thread owns it. Exception
问题描述
private void Thread1_Exe()
{
try
{
int sleepValT1 = Convert.ToInt32(listBoxT2.SelectedValue);
int StartLoop = 0;
int EndLoop = 10000;
for (int i = StartLoop; i <= EndLoop; i++)
{
Dispatcher.BeginInvoke(
new Action(() => listboxE1.Items.Add("T1: Execution Count> " + i.ToString())));
Thread.Sleep(sleepValT1);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
我试图在不同的线程上调用上述函数
I was trying to call the above function on different thread
private void thread1_Click(object sender, RoutedEventArgs e)
{
threadBtn1.IsEnabled = false;
Thread t1 = new Thread(new ThreadStart(Thread1_Exe));
t1.Start();
}
但是当我从列表框中选择值并尝试将其保存在变量中然后尝试将该值传递给
But the Exception occurs when i select the value from the listbox and try to save it in the variable and then try to pass that value in
Thread.Sleep()
我总是得到这个异常,不同的线程拥有这个对象.尝试了很多东西.请帮我找出我做错的地方.
I always get this Exception that the different thread owns this object. Tried Many things. Please help me out where i am doing mistake.
谢谢
推荐答案
您的问题是您正在从后台线程访问 UI 控件.UI 往往是单线程的,因此您需要协调对控件的访问,以便始终通过 UI 线程进行访问.
Your problem is that you are accessing a UI control from a background thread. UIs tend to be single-threaded, so you need to coordinate access to the controls so that it always happens via the UI thread.
您可以做的最简单的事情是在启动新线程之前从列表框中读取值:
The simplest thing you can do is read the value from your listbox before firing up the new thread:
private void Thread1_Exe(int sleepVal)
{
try
{
int StartLoop = 0;
int EndLoop = 10000;
for (int i = StartLoop; i <= EndLoop; i++)
{
Dispatcher.BeginInvoke(
new Action(() =>
listboxE1.Items.Add("T1: Execution Count> " + i.ToString())));
Thread.Sleep(sleepVal);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
private void thread1_Click(object sender, RoutedEventArgs e)
{
threadBtn1.IsEnabled = false;
int sleepValT1 = Convert.ToInt32(listBoxT1.SelectedValue);
Thread t1 = new Thread(() => Thread1_Exe(sleepValT1));
t1.Start();
}
其他替代方法包括通过其调度程序线程访问控件:
Other alternatives include accessing the control via its dispatcher thread:
private void Thread1_Exe()
{
try
{
int sleepValT1;
listBoxT1.Dispatcher.Invoke(() => sleepValT1 = Convert.ToInt32(listBoxT1.SelectedValue));
int StartLoop = 0;
int EndLoop = 10000;
for (int i = StartLoop; i <= EndLoop; i++)
{
Dispatcher.BeginInvoke(
new Action(() =>
listboxE1.Items.Add("T1: Execution Count> " + i.ToString())));
Thread.Sleep(sleepValT1);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
private void thread1_Click(object sender, RoutedEventArgs e)
{
threadBtn1.IsEnabled = false;
Thread t1 = new Thread(() => Thread1_Exe());
t1.Start();
}
通常,您确实希望尽可能避免在线程之间共享可变状态,以使事情更易于推理.
Generally, you really want to avoid sharing mutable state between threads as much as possible, to keep things easier to reason about.
这篇关于调用线程无法访问此对象,因为其他线程拥有它.例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!