Winforms and non-STA Threads

Recently I came across a request for a workaround the STAThread problem from a customer. He wanted to show a Save file dialog from within a CallExternalMethod activity. Since this thread is owned by the workflow instance there is no way to set the apartment state of this thread to ApartmentState.STA. This snippet works around the problem. ( Although In general using block UI elements like this inside of workflows is not recommended)

string fileSavePath;
Thread newThread = new Thread(delegate()
          using (SaveFileDialog saveFileDialog = new SaveFileDialog())
                 if (saveFileDialog.ShowDialog() == DialogResult.OK)
                           fileSavePath = saveFileDialog.FileName;

This entry was posted in Windows Workflow Foundation. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s