From fd7808d7e2a9f92f71b54b6670e3006ea234cdec Mon Sep 17 00:00:00 2001 From: tux Date: Wed, 2 Jul 2025 06:30:32 +0530 Subject: [PATCH] fix: async func not waiting --- ui/controls/ChatUserControl.axaml.cs | 39 +++++++++++++++++----------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/ui/controls/ChatUserControl.axaml.cs b/ui/controls/ChatUserControl.axaml.cs index a109b7d..7b8bf52 100644 --- a/ui/controls/ChatUserControl.axaml.cs +++ b/ui/controls/ChatUserControl.axaml.cs @@ -25,25 +25,34 @@ public partial class ChatUserControl : UserControl public async void SendScreenshot() { - var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss"); - var fileName = $"screenshot_{timestamp}.png"; - var filePath = Path.Combine(Environment.CurrentDirectory, fileName); + try + { - var screenshot = await ScreenCapture.CaptureScreenAsync(filePath); - if (!screenshot) return; + var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss"); + var fileName = $"screenshot_{timestamp}.png"; + var filePath = Path.Combine(Environment.CurrentDirectory, fileName); - using Stream imageStream = File.OpenRead(filePath); - BinaryData imageBytes = BinaryData.FromStream(imageStream); + var screenshot = await ScreenCapture.CaptureScreenAsync(filePath); + if (!screenshot) throw new Exception("Failed to capture screenshot"); - List messages = - [ - new UserChatMessage( - ChatMessageContentPart.CreateTextPart("I'm attaching a screenshot of a problem. I want you to read it and give me the appropriate answer."), - ChatMessageContentPart.CreateImagePart(imageBytes, "image/png") - ) - ]; + await using Stream imageStream = File.OpenRead(filePath); + var imageBytes = await BinaryData.FromStreamAsync(imageStream); - await ProcessChatStreamAsync(messages); + List messages = + [ + new UserChatMessage( + ChatMessageContentPart.CreateTextPart( + "I'm attaching a screenshot of a problem. I want you to read it and give me the appropriate answer."), + ChatMessageContentPart.CreateImagePart(imageBytes, "image/png") + ) + ]; + + await ProcessChatStreamAsync(messages); + } + catch (Exception err) + { + ResultBlock.Text = err.Message; + } } private async void PromptBox_OnKeyDown(object? sender, KeyEventArgs e)