1

I simply tried this:

    public class FooJob : IJob
{
    public FooJob() { }

    public void Execute(JobExecutionContext context)
    {
        Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
    }
}

But it produces InvalidOperationException. Ideas?

WhiteKnight
  • 4,408
  • 4
  • 33
  • 37
Ashley Simpson
  • 373
  • 1
  • 5
  • 16

1 Answers1

1

The thread has already been allocated from the thread pool so it can't become a thread created in an STA. What you can do is launch an STA thread from your IJob.Execute method.

public void Execute(JobExecutionContext context)
{
    Thread t= new Thread(DoSomeWork);
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
    t.Join();
}
AndyM
  • 1,017
  • 1
  • 6
  • 11