I know this is old, but Damien's answer and the subsequent comment helped me with my issue. I have a console app in which I need to call async
methods that at some point may need STA execution to use the OpenFileDialog
.
Here is my resulting code in case it helps others (or just my future self).
1. Created Extension Method for running thread as STA
public static class Extensions
{
public static void RunSTA(this Thread thread)
{
thread.SetApartmentState(ApartmentState.STA); // Configure for STA
thread.Start(); // Start running STA thread for action
thread.Join(); // Sync back to running thread
}
}
2. Created async main
method with await
to application method (no [STAThread]
attribute).
class Program
{
static async Task Main(string[] args)
{
await App.Get().Run(args);
}
}
3. Use extension method to wrap OpenFileDialog
call with STA
public string[] GetFilesFromDialog(string filter, bool? restoreDirectory = true, bool? allowMultiSelect = true)
{
var results = new string[] { };
new Thread(() =>
{
using (var dialog = new OpenFileDialog())
{
dialog.Filter = filter;
dialog.RestoreDirectory = restoreDirectory ?? true;
dialog.Multiselect = allowMultiSelect ?? true;
if (dialog.ShowDialog() != DialogResult.OK)
return; // Nothing selected
results = dialog.FileNames;
}
}).RunSTA();
return results;
}
ThreadStateException
since, by definition, the current thread has already started. – Esmerolda