Wpf 如何知道等待时任务何时完成?
我想这样做:Wpf 如何知道等待时任务何时完成?,wpf,task-parallel-library,Wpf,Task Parallel Library,我想这样做: Loading = true; var data = await DataProvider.DoWorkAsync(); Loading = false; // but have this not get set until the data is loaded <StackPanel> <ProgressBar IsIndeterminate="{Binding Loading}" Height="50" Width="100" /> &
Loading = true;
var data = await DataProvider.DoWorkAsync();
Loading = false; // but have this not get set until the data is loaded
<StackPanel>
<ProgressBar IsIndeterminate="{Binding Loading}" Height="50" Width="100" />
<Button Content="Test" Click="Button_Click" />
<TextBlock x:Name="txt" />
</StackPanel>
加载只是一个绑定到WPF应用程序中的进度指示器的属性,数据提供者被注入。我只是想知道数据何时完成加载,以便更新进度指示器
编辑:
将DoWork()
更新为doworksync()
,与我的代码相同。doworksync()
和DoWork()
调用如下所示
public Task<DataStructure> DoWorkAsync()
{
return Task.Run(() => DoWork());
}
public DataStructure DoWork()
{
var dataStructure = new DataStructure();
// Heavy lifting here
return dataStructure;
}
公共任务DoWorkAsync()
{
返回任务。运行(()=>DoWork());
}
公共数据结构DoWork()
{
var dataStructure=新数据结构();
//这里很重
返回数据结构;
}
上面的代码不起作用,而且由于我实际上没有在等待时返回任务,因此我不确定ContinueWith()
是否是正确的方法
做这件事最好的方法是什么?看起来应该可以 尝试创建一个新的WPF应用程序,其代码隐藏中仅包含以下代码:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
Loading = true;
var data = await DoWorkAsync();
txt.Text = data.Name;
Loading = false;
}
public Task<DataStructure> DoWorkAsync()
{
return Task.Run(() => DoWork());
}
public DataStructure DoWork()
{
var dataStructure = new DataStructure();
System.Threading.Thread.Sleep(5000);
dataStructure.Name = "Test";
return dataStructure;
}
public class DataStructure
{
public string Name { get; set; }
}
private bool _loading = false;
public bool Loading
{
get { return _loading; }
set
{
_loading = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Loading"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public分部类主窗口:窗口,INotifyPropertyChanged
{
公共主窗口()
{
初始化组件();
this.DataContext=this;
}
专用异步无效按钮\u单击(对象发送方,路由目标)
{
加载=真;
var data=await-doworksync();
txt.Text=data.Name;
加载=假;
}
公共任务DoWorkAsync()
{
返回任务。运行(()=>DoWork());
}
公共数据结构DoWork()
{
var dataStructure=新数据结构();
系统线程线程睡眠(5000);
dataStructure.Name=“Test”;
返回数据结构;
}
公共类数据结构
{
公共字符串名称{get;set;}
}
私有bool_加载=false;
公共布尔装载
{
获取{return\u loading;}
设置
{
_载荷=值;
if(PropertyChanged!=null)
PropertyChanged(即新PropertyChangedEventArgs(“加载”);
}
}
公共事件属性更改事件处理程序属性更改;
}
}
..和具有以下内容的XAML文件:
Loading = true;
var data = await DataProvider.DoWorkAsync();
Loading = false; // but have this not get set until the data is loaded
<StackPanel>
<ProgressBar IsIndeterminate="{Binding Loading}" Height="50" Width="100" />
<Button Content="Test" Click="Button_Click" />
<TextBlock x:Name="txt" />
</StackPanel>
您应该看到button eventhandler将异步调用DoWorkAsync
方法并启动progressbar。任务
完成后,文本块
将更新,进度条将停止
也许你可以用这个例子来说明你在做什么不同?这段代码当然可以工作;这就是方法。听起来像是
DoWork
可能没有正确编码?是否有任何DoWorkCompleted事件?从概念上讲,等待后的代码是一个延续,就像ContinueWith一样。您应该展示如何实现DoWork
。方法的签名是什么?您是否使用了async void
而不是async Task
?谢谢大家,听起来应该可以。。。我还将使用DoWork()
调用进行更新。。。如果这看起来没问题,那么肯定还有别的事情发生。