Set ApartmentState on a Task

前端 未结 6 763
轮回少年
轮回少年 2020-11-27 03:02

I am trying to set the apartment state on a task but see no option in doing this. Is there a way to do this using a Task?

for (int i = 0; i < zom.Count; i         


        
6条回答
  •  渐次进展
    2020-11-27 03:04

    You could for example make a new task as follows:

           try
            {
                Task reportTask = Task.Factory.StartNew(
                    () =>
                    {
                        Report report = new Report(this._manager);
                        report.ExporterPDF();
                    }
                    , CancellationToken.None
                    , TaskCreationOptions.None
                    , TaskScheduler.FromCurrentSynchronizationContext()
                    );
    
                reportTask.Wait();
            }
            catch (AggregateException ex)
            {
                foreach(var exception in ex.InnerExceptions)
                {
                    throw ex.InnerException;
                }
            }
    

提交回复
热议问题