Skip to main content
 首页 » 编程设计

c#之调用线程必须是STA,因为很多UI组件都需要这个WPF

2024年02月20日14jyk

我正在使用 WPF Toolkit 提供的 MessageBox .我得到了错误

调用线程必须是STA,因为很多UI组件都需要这个

new Thread(new ThreadStart(delegate 
{ 
    MessageBox.Show("Opeartion could not be completed. Please try again.","Error",MessageBoxButton.OK,MessageBoxImage.Error); 
})).Start(); 

在这种情况下如何设置 ApartmentState

编辑: 我正在尝试使用 WPF 工具包的 MessageBox 控件显示无模式 MessageBox。 到目前为止,我的代码如下:

void SomeFunction() 
{ 
// calls to some UI, and processing and then 
 
var th = new Thread(new ThreadStart(delegate 
                                        { 
                                           MessageBox.Show("Opeartion could not be completed. Please try again.", 
                                                "Error", MessageBoxButton.OK, MessageBoxImage.Error); 
                                        })); 
 
                                        th.SetApartmentState(ApartmentState.STA); 
                                        th.Start(); 
                                    } 
}