我正在使用SAP .NET Connector 3.0并试图使用单独的线程登录,因此我可以使UI显示一种登录动画。在Task.Run中使用await,但UI仍然挂起几秒钟?
我使用异步和等待来启动登录,但用户界面在登录过程中挂起约10秒钟。
这里是代码,它很粗糙,因为我正在快速起草一个程序。
async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Busy.Visibility = System.Windows.Visibility.Visible; // Shows progress animation
if (await SAPLogin()) // Waits for login to finish, will always be true at the moment
{
await GetData(); // does things with sap
Busy.Visibility = System.Windows.Visibility.Collapsed; // Hides progress animation
}
}
private Task<bool> SAPLogin()
{
bool LoggedIn = true;
return Task.Run(() =>
{
Backend = new BackendConfig();
RfcDestinationManager.RegisterDestinationConfiguration(Backend);
SapRfcDestination = RfcDestinationManager.GetDestination(MyServer); // MyServer is just a string containing sever name
SapRap = SapRfcDestination.Repository;
BapiMD04 = SapRap.CreateFunction("MD_STOCK_REQUIREMENTS_LIST_API");
BapiMD04.SetValue("WERKS", "140");
return LoggedIn;
});
}
我只能想象任务中的某些内容正在使用UI吗?
编辑1: 对不起忘了解释GetData()
做什么。 GetData()
在SAP中运行各种报告(大量代码)。在视觉上,我知道它的存在是因为我的小登录动画将从“登录”变为“抓取数据”。 当我看到UI挂起时,我发现它是在“登录”阶段。登录动画有一个简单的圆圈旋转。这会在登录过程中停止,然后在大约5秒后继续。
编辑2: 悬似乎在这行这里occour
SapRfcDestination = RfcDestinationManager.GetDestination(MyServer);
编辑3: 添加在该点暂停的应用程序时,我看到的UI线程挂起的照片。
GetData是做什么的? – usr
对不起,我将编辑原文,让所有人都能看到 – Gaz83
注释掉整个'if'使100%确定它导致了这个(我认为它不会)。 'if(false &&等待SAPLogin())'。 – usr