C# BusyIndicator未从异步方法显示
我正在尝试升级以前基于BackgroundWorker的方法,以使用库中的异步调用。我遇到的问题是,busy指示器不再适用于新的异步功能(我是新手)。忙碌指示器不显示,GUI在操作执行时冻结,然后在操作完成时解冻。我不确定我做错了什么 XAML代码:C# BusyIndicator未从异步方法显示,c#,asynchronous,busyindicator,C#,Asynchronous,Busyindicator,我正在尝试升级以前基于BackgroundWorker的方法,以使用库中的异步调用。我遇到的问题是,busy指示器不再适用于新的异步功能(我是新手)。忙碌指示器不显示,GUI在操作执行时冻结,然后在操作完成时解冻。我不确定我做错了什么 XAML代码: <Border BorderBrush="Black" BorderThickness="1" CornerRadius="5,5,15,15" Background="#FFA8A9AC" Grid.Column="1" Grid.Row=
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="5,5,15,15" Background="#FFA8A9AC" Grid.Column="1" Grid.Row="1">
<Grid x:Name="UIGrid">
<toolkit:BusyIndicator x:Name="BusyIndicator" IsBusy="{Binding Instance.IsBusy}" BusyContent="{Binding Instance.BusyMessage, TargetNullValue='Please wait...'}" >
<Grid x:Name="ScreenGrid" />
</toolkit:BusyIndicator>
</Grid>
</Border>
编辑:
要回复@Jon Skeet,以下是CreateReport的重要部分:
public async Task CreateReport()
{
SystemLog.Trace(this);
var internalReportId = GenerateReportId();
var rpt = await ReportFactory.Create();
rpt.InternalReportId = internalReportId;
rpt.EnableNotifications = true;
ReportRepository = new ReportRepository();
await SaveReport(rpt);
// Set the active report.
BusinessLogic.Instance.ActiveReport = rpt;
await AssignReportNumber(MediViewData.Instance.ActiveReport);
}
public async static Task<Report> Create()
{
var rpt = new Report();
/** Set several fields to default values. */
return rpt;
}
public异步任务CreateReport()
{
SystemLog.Trace(this);
var internalReportId=generaterereportid();
var rpt=await ReportFactory.Create();
rpt.InternalReportId=InternalReportId;
rpt.EnableNotifications=true;
ReportRepository=newreportrepository();
等待保存报告(rpt);
//设置活动报告。
BusinessLogic.Instance.ActiveReport=rpt;
等待AssignReportNumber(MediViewData.Instance.ActiveReport);
}
公共异步静态任务Create()
{
var rpt=新报告();
/**将多个字段设置为默认值*/
返回rpt;
}
没有足够的信息为您提供答案代码,但通常:
首先,认识到async
不会在后台线程上运行代码。它所做的只是创建一个小的状态机来启用wait
操作。同样,await
不会在后台线程上运行代码
如果您有CPU密集型操作,则应使用
wait Task.Run(..)
将其推送到后台线程。任何类型的数据绑定都被视为一种UI操作,不应该在调用任务的过程中进行。运行ReportManager.CreateReport
做什么?如果它只是同步执行,那么它将不会帮助您…我添加了一些代码,希望能够澄清它在做什么。有什么事情是错的吗?哪一点需要时间?是不是SaveReport
?那是什么样子的?基本上,我怀疑你实际上是在用同步调用做大部分工作。最大的事情是分配ActiveReport。这更新了所有的绑定。我想这已经消除了我的很多困惑。异步后台处理与实际后台处理之间的差异。您可能希望使用Task.Factory.StartNew
,它提供了更多选项。@heap1:StartNew
<代码>任务。运行
应该是默认选择,只有在必要时才使用启动新的。
public async Task CreateReport()
{
SystemLog.Trace(this);
var internalReportId = GenerateReportId();
var rpt = await ReportFactory.Create();
rpt.InternalReportId = internalReportId;
rpt.EnableNotifications = true;
ReportRepository = new ReportRepository();
await SaveReport(rpt);
// Set the active report.
BusinessLogic.Instance.ActiveReport = rpt;
await AssignReportNumber(MediViewData.Instance.ActiveReport);
}
public async static Task<Report> Create()
{
var rpt = new Report();
/** Set several fields to default values. */
return rpt;
}