fix: async func not waiting

This commit is contained in:
tux
2025-07-02 06:30:32 +05:30
parent d558405afe
commit fd7808d7e2

View File

@@ -25,26 +25,35 @@ public partial class ChatUserControl : UserControl
public async void SendScreenshot() public async void SendScreenshot()
{ {
try
{
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss"); var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var fileName = $"screenshot_{timestamp}.png"; var fileName = $"screenshot_{timestamp}.png";
var filePath = Path.Combine(Environment.CurrentDirectory, fileName); var filePath = Path.Combine(Environment.CurrentDirectory, fileName);
var screenshot = await ScreenCapture.CaptureScreenAsync(filePath); var screenshot = await ScreenCapture.CaptureScreenAsync(filePath);
if (!screenshot) return; if (!screenshot) throw new Exception("Failed to capture screenshot");
using Stream imageStream = File.OpenRead(filePath); await using Stream imageStream = File.OpenRead(filePath);
BinaryData imageBytes = BinaryData.FromStream(imageStream); var imageBytes = await BinaryData.FromStreamAsync(imageStream);
List<ChatMessage> messages = List<ChatMessage> messages =
[ [
new UserChatMessage( 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.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") ChatMessageContentPart.CreateImagePart(imageBytes, "image/png")
) )
]; ];
await ProcessChatStreamAsync(messages); await ProcessChatStreamAsync(messages);
} }
catch (Exception err)
{
ResultBlock.Text = err.Message;
}
}
private async void PromptBox_OnKeyDown(object? sender, KeyEventArgs e) private async void PromptBox_OnKeyDown(object? sender, KeyEventArgs e)
{ {