I have a WinForm application.
error info:Error message: System.Threading.ThreadStateException: "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked. This exception is only raised if a debugger is attached to the process." Some of the code is as follows: 1.Partial code of class Program.cs: [STAThread] static async Task Main(string[] args) {
RadSplashScreenManager.ContentCreated += RadSplashScreenManager_ContentCreated;
RadSplashScreenSettings f = new RadSplashScreenSettings()...
var services = new ServiceCollection(); ... services.AddTransient<RadFrmMain>(); await using var sp = services.BuildServiceProvider(new ServiceProviderOptions { ValidateScopes = true, ValidateOnBuild = true }); Application.Run(sp.GetRequiredService<RadFrmMain>()); } 2.Partial code inside RadFrmMain.cs: radMenuOpenProject.Name = "radMenuOpenProject"; radMenuOpenProject.Text = "Open Project"; radMenuOpenProject.Click += radMenuOpenProject_Click; private async void radMenuOpenProject_Click(object sender, EventArgs e) { await OpenProjectAsync(); } private async Task OpenProjectAsync() { string? pickedFile = await InvokeOnUiAsync(() => { System.Diagnostics.Debug.Assert( System.Threading.Thread.CurrentThread.GetApartmentState() == System.Threading.ApartmentState.STA, "OpenFileDialog must be called on STA"); var root = string.IsNullOrWhiteSpace(_settings.SqliteRoot) ? "projects_db" : _settings.SqliteRoot; var start = LastPathStore.LoadOr(root); using var ofd = new OpenFileDialog { Title = "Open Project", Filter = "Project files (*.project.json;*.db)|*.project.json;*.db|JSON files (*.project.json)|*.project.json|SQLite (*.db)|*.db|All files (*.*)|*.*", Multiselect = false, InitialDirectory = Directory.Exists(start) ? start : root }; return ofd.ShowDialog(this) == DialogResult.OK ? ofd.FileName : null; //Error message: System.Threading.ThreadStateException: "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked. This exception is only raised if a debugger is attached to the process." }); } Previously, when using: [STAThread] static void Main() it worked normally, but the project must use static async Task Main(string[] args). How to handle this?