C# WinForms: How to set Main function STAThreadAttribute

前端 未结 5 1593
轮回少年
轮回少年 2020-11-27 06:55

I get the following exception when calling saveFileDialog.ShowDialog() in a background thread:

Current thread must be set to single thr

5条回答
  •  有刺的猬
    2020-11-27 07:14

    this should work if you are creating the thread in which you call the showDialog:

    var thread = new Thread(new ParameterizedThreadStart(param => { saveFileDialog.ShowDialog(); }));
     thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    

提交回复
热议问题