gpt4 book ai didi

所有代码运行后显示的 C# 异步窗口

转载 作者:行者123 更新时间:2023-11-30 16:47:41 25 4
gpt4 key购买 nike

我正在尝试在我的 WPF 应用程序中实现一个“加载窗口”,它应该在客户端想要向服务器发送消息时显示,然后应该在收到响应后关闭。当我使用异步方法时,这应该不是什么大问题,但不知何故窗口显示 after 收到响应并立即关闭(如预期)。

下面是一些代码片段

    public async Task<CommunicationMessage> SendAndReadMessageSync(string message, bool showLoadingScreen = true) {

// Show loading screen if needed
if (showLoadingScreen == true) {
await Globals.MessageController.ShowLoadingScreen(); // This method is the second one
}

// Send a message to the server
SendMessageSync(message);

// Receive the response from the server
CommunicationMessage response = ReadMessageSync();

// Close loading screen
if (showLoadingScreen == true) {
await Globals.MessageController.CloseLoadingScreen();
}

// return the response
return response;
}


public async Task ShowLoadingScreen(string title = "Lade Daten", string message = "Einen Moment Geduld bitte") {

await Dispatcher.BeginInvoke(new Action(async delegate {

// Create the loading screen
LoadingScreen ls = new LoadingScreen(title, message) {
IsModal = true
};

// Store the screen inside list
loadingScreens.Add(ls);

// Show the screen
await this.ShowChildWindowAsync(ls, ChildWindowManager.OverlayFillBehavior.FullWindow);
}));
}

我对执行的想法是,加载屏幕将独立于 SendMessageSync() 方法显示,该方法可能会持续 10 秒。客户端收到响应后,加载屏幕将关闭。

我是做错了什么还是忘记了什么?如果您需要更多代码,请告诉我

非常感谢!

最佳答案

我建议将消息​​的发送封装在任务中。现在它将在 UI 线程上运行。所以看起来 UI 线程很忙,根本无法显示您的等待窗口。

关于所有代码运行后显示的 C# 异步窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39204409/

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