Thank you for the clarification. You have to change the apartment model of the thread calling the method causing this problem. You need to change it to STA as required; there is no another way. There are at least two ways of doing that.
You cannot do it on your background worker. I actually almost never use
BackgroundWalker
, by this and a number of other reasons. I would create a "permanent" thread which I would reuse during the lifetime of the application.
Thread myThread = new Thread(threadMethod);
myThread.SetApartmentState(ApartmentState.STA);
myThread.Start();
As you can see, apartment thread is set before the thread start, in some other thread. This is also a must: a thread cannot change its own apartment state. See also my past answer on working with such threads and their reuse:
Making Code Thread Safe[
^],
Running exactly one job/thread/process in webservice that will never get terminated (asp.net)[
^],
How to pass ref parameter to the thread[
^],
Change parameters of thread (producer) after it is started[
^],
MultiThreading in C#[
^].
Another way is this, you can define the apartment model of your application to STA. As this is a
System.Windows.Forms
application, in can work with any apartment model (and WPF can work only with STA, so you can use any of them). This is done by setting the usual attribute for the entry-point method:
[System.STAThread]
static void Main(string[] args) {
}
Please see:
https://msdn.microsoft.com/en-us/library/system.stathreadattribute%28v=vs.110%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/system.mtathreadattribute(v=vs.110).aspx[
^].
Note, "
single-threaded apartment" absolutely does not mean impossibility of working with multiple threads. These names are just confusing. For understanding background, please see:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms693344.aspx[
^],
https://msdn.microsoft.com/en-us/library/windows/desktop/ms680112.aspx[
^],
https://msdn.microsoft.com/en-us/library/windows/desktop/ms693421.aspx[
^].
—SA