كيفية جعل وظيفة Quartz.net لتشغيلها في شقة واحدة الخيوط؟

StackOverflow https://stackoverflow.com/questions/1449393

سؤال

حاولت ببساطة هذا:

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

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

لكنها تنتج InvalistoperationException. الأفكار؟

هل كانت مفيدة؟

المحلول

تم تخصيص مؤشر الترابط بالفعل من تجمع الخيط حتى لا يمكن أن يصبح مؤشر ترابط تم إنشاؤه في STA. ما يمكنك القيام به هو إطلاق مؤشر ترابط STA من طريقة iJob.Execute الخاصة بك.

public void Execute(JobExecutionContext context)
{
    Thread t= new Thread(DoSomeWork);
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
    t.Join();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top