Possible Duplicate:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on可能的重复:交叉线程操作无效:从线程以外的线程访问控制。
Okay, I know why this is giving me this error:
好的,我知道为什么会有这样的错误
Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
跨线程操作无效:控制“Form1”从线程以外的线程访问。
But... How can I make this workable?
但是…我怎样才能使它可行呢?
System.Threading.Thread t = new System.Threading.Thread(()=>
{
// do really hard work and then...
listView1.Items.Add(lots of items);
lots more UI work
});
t.Start();
I don't care when, or how the Thread finishes, so I don't really care about anything fancy or over complicated atm, unless it'll make things much easier when working with the UI in a new Thread.
我不关心线程何时结束,也不关心线程如何结束,所以我不关心任何花哨的东西,也不关心复杂的atm,除非在新的线程中使用UI会使事情变得更容易。
2 个解决方案
#1
23
You can't. UI operations must be performed on the owning thread. Period.
你不能。UI操作必须在拥有的线程上执行。时期。
What you could do, is create all those items on a child thread, then call Control.Invoke
and do your databinding there.
您可以做的是,在子线程上创建所有这些项,然后调用Control。在那里调用并执行数据库。
Or use a BackgroundWorker
或者使用BackgroundWorker
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (s, e) => { /* create items */ };
bw.RunWorkerCompleted += (s, e) => { /* databind UI element*/ };
bw.RunWorkerAsync();
#2
13
When you access the from's property from another thread, this exception is thrown. To work around this problem there's at least 2 options.
当您从另一个线程访问from的属性时,将抛出此异常。要解决这个问题,至少有两个选择。
-
Telling Control to don't throw these exceptions (which is not recommended):
告诉控件不要抛出这些异常(不建议抛出):
Control.CheckForIllegalCrossThreadCalls = false;
控制。CheckForIllegalCrossThreadCalls = false;
-
Using threadsafe functions:
使用线程安全的功能:
private void ThreadSafeFunction(int intVal, bool boolVal) { if (this.InvokeRequired) { this.Invoke( new MethodInvoker( delegate() { ThreadSafeFunction(intVal, boolVal); })); } else { //use intval and boolval } }
#1
23
You can't. UI operations must be performed on the owning thread. Period.
你不能。UI操作必须在拥有的线程上执行。时期。
What you could do, is create all those items on a child thread, then call Control.Invoke
and do your databinding there.
您可以做的是,在子线程上创建所有这些项,然后调用Control。在那里调用并执行数据库。
Or use a BackgroundWorker
或者使用BackgroundWorker
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (s, e) => { /* create items */ };
bw.RunWorkerCompleted += (s, e) => { /* databind UI element*/ };
bw.RunWorkerAsync();
#2
13
When you access the from's property from another thread, this exception is thrown. To work around this problem there's at least 2 options.
当您从另一个线程访问from的属性时,将抛出此异常。要解决这个问题,至少有两个选择。
-
Telling Control to don't throw these exceptions (which is not recommended):
告诉控件不要抛出这些异常(不建议抛出):
Control.CheckForIllegalCrossThreadCalls = false;
控制。CheckForIllegalCrossThreadCalls = false;
-
Using threadsafe functions:
使用线程安全的功能:
private void ThreadSafeFunction(int intVal, bool boolVal) { if (this.InvokeRequired) { this.Invoke( new MethodInvoker( delegate() { ThreadSafeFunction(intVal, boolVal); })); } else { //use intval and boolval } }