使用Invoke Async Deadlock更新UI组件

时间:2018-04-10 16:36:56

标签: c# user-interface begininvoke sta

我正在使用devexpress和他们的PivotGrid控件来构建一个透视网格。我不关心显示它我没有显示实际的UI,我想要PivotGrid所以我可以导出它。

因此,我在一个单独的STA线程上创建和更新数据透视网格对象,否则它将无法工作。我通过以下代码执行处理数据检索的方法:

 PivotGridControl pivotGrid = null;
 Thread thread = new Thread(() => { pivotGrid = _reportProcessor.RunReport(currentReport.ReportId).Result; });
 thread.SetApartmentState(ApartmentState.STA);
 thread.Start();
 thread.Join();

RunReport方法无效的部分如下:

 public async Task<PivotGridControl> RunReport(int reportId)
    {
        //var pivotGrid = new PivotGridFactory().GetPivotGrid();

        var pivotGrid = new PivotGridControl();

        //Get report info
        var report = await _reportReferenceService.GetReportReference(reportId);

        //Get data definition
        var dataDefinition = await _reportDataDefService.GetDataDefinition(report.DataDefinitionId);

        //Get fields
        var fields = await _reportFieldService.GetFields(FieldSetId);

        //Get layout
        var layout = await _reportLayoutService.GetLayout(report.LayoutId);
        var layoutStream = StreamUtils.StringToStream(layout.Definition);
        var collapseStream = StreamUtils.Base64StringToStream(layout.CollapseState);
        await pivotGrid.Dispatcher.InvokeAsync(delegate
        {
            pivotGrid.BeginUpdate();
            pivotGrid.Fields.Clear();
            pivotGrid.Fields.Where(x => x.Visible).ForEach(x => x.Visible = false);
            pivotGrid.RestoreLayoutFromStream(layoutStream);
            pivotGrid.RestoreCollapsedStateFromStream(collapseStream);
            pivotGrid.AllowConditionalFormattingMenu = true;
            pivotGrid.AllowConditionalFormattingManager = true;
            pivotGrid.EndUpdate();
        });

        //Get field spec
        var fieldSpec = _reportFieldService.GetFieldSpecification(pivotGrid.Fields);

        //Get report data 
        var reportData = await _reportDataService.GetReportData(fieldSpec, dataDefinition.FundDataSpecification, dataDefinition.DifferenceSpecification, dataDefinition.TimeSeriesSpecification, dataDefinition.RiskDataSpecification, null);

        //Set pivot data
        pivotGrid.BeginUpdate();
        pivotGrid.DataSource = reportData;
        pivotGrid.EndUpdate();

        return pivotGrid;

    }

由于这是锁和代理永远不会运行,我假设某处存在死锁,但我很惊讶代表根本没有运行。

0 个答案:

没有答案