【发布时间】:2020-05-30 10:52:04
【问题描述】:
我正在使用一个在运行长 SQL 语句时应该显示的对象;我使用 XAML 中的布尔值和数据绑定来触发它;
<window>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
</Window.Resources>
<Controls:ProgressRing x:Name="PRwaiting" Visibility="{Binding Path=isBusy,Converter={StaticResource BoolToVis}}"/>
</window>
在代码隐藏中;
public async void DoSomething()
isBusy = true;
await LongRunninSQL();
}
private async Task<bool> LongRunninSQL() {
//The isBusy ProgressRing object is visible when I use this delay with a normal SQL statement
//await Task.Delay(3000);
//Yet, the GUI is blocked when this is executed. (and no progressring is displayed)
string SQL = $"select benchmark(9999999, md5('when will it end?')) AS Benchmark";
Collection = await DBC.SelectAsync(SQL);
DBC.SelectAsync:
public async Task<DataTable> SelectAsync(string query)
{
var dataTable = new DataTable();
if (this.OpenConnection() == true)
{
using (MySqlCommand cmd = new MySqlCommand(query, connection))
{
cmd.CommandText = query;
using (var dataReader = await cmd.ExecuteReaderAsync())
{
dataTable.Load(dataReader);
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return dataTable;
}
}
}
return dataTable;
}
即使使用了 await 和 async,我如何才能找出 SQL 语句冻结 GUI 的原因?
【问题讨论】:
标签: c# mysql wpf async-await