跨线程 UI - 动态控件
本文关键字:动态控件 UI 线程 | 更新日期: 2023-09-27 18:37:01
我的WinForms应用程序需要一个加载面板。我正在使用此处找到的示例
(flowLayoutPanel1 位于 UI 线程上 示例中替换 Thread.Sleep 的异步委托代码类似于下面的代码。我收到跨线程错误,将我的动态控件添加到流面板)
AsyncProcessDelegate d = delegate() {
flowLayoutPanel1.Controls.Clear();
Panel p1 = new Panel();
Button btn1 = new Button();
p1.Controls.Add(btn1);
//tried to access from another thread
flowLayoutPanel1.Controls.Add(p1);
// Just in case it's not clear, I already know this does not work
// The question is how can I make the example wait for my UI to load
// if I can't do the dynamic control creation as shown.
};
RunAsyncOperation(d);
这不是
一个好的做法,但这绝对可以解决您的问题
this.Control.CheckForIllegalCrossThreadCalls=false;