gpt4 book ai didi

c# - 在 c# 中使用 async 和 await 时 UI 会被阻塞

转载 作者:行者123 更新时间:2023-11-30 23:07:17 24 4
gpt4 key购买 nike

这里我需要将一些数据从本地服务器导出到 Excel 中。我正在对该方法使用 async 调用。但是,它在导出数据时仍然会阻塞 UI。

我可以知道 UI 被阻塞的原因吗?

另外,我需要一些说明

1)await keyword is using to wait some time until the process is completed, then what is the difference b/w synchronous and asynchronous process.

2)If one method is declared as Async task, then all inner methods are performing as asynchronous?

3)While Executing inner methods(method1,method2,method3), method3 will be depends upon method1. So, put await keyword for method1. Am I right?

代码片段:

private async void ConvertExcel(object sender)
{
var excelEngine = new ExcelEngine();
var application = excelEngine.Excel;
var newbookWorkbook = application.Workbooks.Create(1);
var work = newbookWorkbook.Worksheets[0];
var openDialog = new SaveFileDialog
{
FilterIndex = 2,
Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2013 Files(*.xlsx)|*.xlsx"
};
if (openDialog.ShowDialog() == true)
{
newbookWorkbook.Version = openDialog.FilterIndex == 1 ? ExcelVersion.Excel97to2003 : ExcelVersion.Excel2013;
}
else
return;
try
{
// This method is used to get the data from server.
var table = GetFullDataAsync();
work.ImportDataTable(table, true, 1, 1, true);
using (Stream stream = openDialog.OpenFile())
{
newbookWorkbook.SaveAs(stream);
}

newbookWorkbook.Close();
excelEngine.Dispose();
}
catch (Exception exception)
{
Console.WriteLine("Exception message: {0}",ex.Message);
}
}

internal async Task<DataTable> GetFullDataAsync()
{
DataTable dataTable = new DataTable();
dataTable = GetDataFromServer(DataEngine.Query,DataEngine.Connection);

dataTable.Locale = CultureInfo.InvariantCulture;
return dataTable;
}


public DataTable GetDataFromServer(string query, DbConnection connection)
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}

var command = connection.CreateCommand();
command.CommandText = query;
command.Connection = connection;

var reader = command.ExecuteReader();
var dataTable = new DataTable
{
Locale = System.Globalization.CultureInfo.InvariantCulture
};
dataTable.Load(reader);
return dataTable;
}

最佳答案

1) await keyword is using for wait some time until the process is completed, then what is the difference b/w synchronous and asynchronous process.

同步:WAITING阻塞线程,因此锁定用户界面
异步:WAITING释放线程,因此不会阻塞 ui

2) If one method is declared as Async task, then all inner methods are performing as asynchronous?

不要假设那是真的。您可能希望是这种情况,但作为开发人员,您可以按照这种方式进行编码。
编译器不会为您强制执行此操作。

在您上面的代码中,答案是否定的;您的代码不是异步的。请注意,您永远不会使用 await 关键字。
您可能需要首先以异步方式访问数据(返回任务),否则 async/await 关键字无法帮助您。

3) While Executing inner methods(method1,method2,method3), method3 will be depends upon method1. So, put await keyword for method1. Am i right?

是的。如果每个方法都返回一个任务,则等待它们全部。通常,当您在一个地方开始使用 await 时,它会在您的其余代码中冒泡,因此您可以按照您的描述不断添加更多等待。

关于c# - 在 c# 中使用 async 和 await 时 UI 会被阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47454699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com