How to solve the "Current thread must be set to single thread apartment (STA) mode" exception in WinForms C#.

How to solve C# exception: 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 on it

If your application runs a piece of code that triggers the following ThreadStateException exception:

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 on it. This exception is only raised if a debugger is attached to the process.'

In this article, we'll explain you briefly how to prevent this exception from appearing in your WinForms project.

Exception example

In our project, we are using the CefSharp library that allows us to create a desktop application with HTML,CSS and JavaScript. One of its features is the possibility of exposing a C# class to a JavaScript object in the window. We have the following class that shows a native dialog to save a file (the showOpenDialog function):

using System; 
using CefSharp.WinForms;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using System.Text;

namespace MyApplication
{
    class WindowsTools
    {
        // Declare a local instance of chromium and the main form in order to execute things from here in the main thread
        private static ChromiumWebBrowser _instanceBrowser = null;
        // The form class needs to be changed according to yours
        private static Form1 _instanceMainForm = null;

        public WindowsTools(ChromiumWebBrowser originalBrowser, Form1 mainForm)
        {
            _instanceBrowser = originalBrowser;
            _instanceMainForm = mainForm;
        }


        // When this method is called like this, it will throw the exception
        public void showOpenFile()
        {
            OpenFileDialog saveFileDialog1 = new OpenFileDialog();

            saveFileDialog1.Filter = "JSON Files (*.json)|*.json";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                
            }
        }
    }
}

If the function is executed from our JavaScript context (the CefSharp threads), the exception will be triggered because we are running the code inside the default Apartment State of CefSharp.

Solution

The main application thread is initialized to ApartmentState.MTA by default. The only way to set the apartment state of the main application thread to ApartmentState.STA is to apply the STAThreadAttribute attribute to the entry point method. In our case, with the method that starts an OpenFileDialog from the class registered in CefSharp, the exception will be thrown if you run the code without changing the Apartment State of the thread.

The following solution is the best one if you don't control the thread creation (like CefSharp), you may create a temporary thread and run the code inside:

using System.Threading;

string selectedPath = "";

Thread t = new Thread((ThreadStart)(() => {
    OpenFileDialog saveFileDialog1 = new OpenFileDialog();

    saveFileDialog1.Filter = "JSON Files (*.json)|*.json";
    saveFileDialog1.FilterIndex = 2;
    saveFileDialog1.RestoreDirectory = true;

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        selectedPath = saveFileDialog1.FileName;
    }
}));

// Run your code from a thread that joins the STA Thread
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();

// e.g C:\Users\MyName\Desktop\myfile.json
Console.WriteLine(selectedPath);

For more information about this exception, we recommend you as well to read this question on StackOverflow.

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors