Frage

Ich versuchte einfach:

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

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

Aber es produziert InvalidOperationException. Ideen?

War es hilfreich?

Lösung

Der Thread wurde bereits aus dem Thread-Pool zugeordnet, so dass es kein Thread erstellt in einem STA werden kann. Was Sie tun können, ist ein STA-Thread von Ihrem IJob.Execute Methode starten.

public void Execute(JobExecutionContext context)
{
    Thread t= new Thread(DoSomeWork);
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
    t.Join();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top