110,565
社区成员
发帖
与我相关
我的任务
分享
<StackPanel Margin="10">
<TextBox x:Name="tb" Width="600" Height="40"></TextBox>
<GroupBox Header="加载数据">
<DataGrid x:Name="dg" Margin="20" Width="600" Height="500" AutoGenerateColumns="True" CanUserAddRows="False">
</DataGrid>
</GroupBox>
<ProgressBar x:Name="pb" Width="500" Height="30" IsIndeterminate="False" Minimum="0" Maximum="100"></ProgressBar>
<TextBlock x:Name="tbTime"></TextBlock>
<Button x:Name="btnClick" Width="120" Height="30" Content="Load" Click="btnClick_Click"></Button>
</StackPanel>
btnClick_Click:
/// <summary>
/// 当前线程调度器
/// </summary>
private readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;
private void btnClick_Click(object sender, RoutedEventArgs e)
{
SlowDudeAsync();
}
/// <summary>
/// 异步读取数据
/// </summary>
public async void SlowDudeAsync()
{
tb.Text = "开始异步\r\n";
pb.IsIndeterminate = true;
// 计算耗时
System.Diagnostics.Stopwatch stop = new System.Diagnostics.Stopwatch();
stop.Start();
await Task.Factory.StartNew(() => SlowDude()); ;
tb.Text += "异步结束";
pb.IsIndeterminate = false;
stop.Stop();
tbTime.Text = "耗时:" + stop.ElapsedMilliseconds + "ms";
}
/// <summary>
/// 读取数据
/// </summary>
public void SlowDude()
{
// 查询数据到DataSet
DataSet dt = DbHelperSQL.ExecuteDataSet("select * from xxxxx", null);
this._dispatcher.BeginInvoke(new Action(() =>
{
dg.ItemsSource = dt.Tables[0].DefaultView;
}));
}
private async void GetShowCellInformationStruct(int QiShuCount, int StartQiShu, int EndQiShu)
{
var slowTask = Task.Factory.StartNew(new Action(() =>
{
curdp.BeginInvoke(DispatcherPriority.Normal, new Action(() => { DataGrid1.ItemsSource = a; }));
}));
await slowTask; //悬挂点。
}
dataGrid1.ItemsSource = a;
你不至于把“数据时间”算做是界面操作时间吧?!!