Domanda

ho semplicemente provato questo:

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

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

ma produce InvalidOperationException. Idee?

È stato utile?

Soluzione

Il filo è già stata assegnata dal pool filo in modo che non può diventare un thread creato in uno STA. Che cosa si può fare è lanciare un thread STA dal tuo metodo IJob.Execute.

public void Execute(JobExecutionContext context)
{
    Thread t= new Thread(DoSomeWork);
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
    t.Join();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top