Auto-translation, HttpClient, Result, await...
This is my code inside an application for translation purposes. It queries the user's OS for the installed language, retrieves the two letter ISO language code, sends it to google along with strings and displays the translated text. However, only in debug mode, this code sometimes throws an access violation reading location exception at "var result" line. I know why that is, because Result is running async and it's blocking the thread until Result is retrieved. Solution appears to be converting the entire method to async Task<string>. Problem is, I racked my brains to find a way to convert Task<string> to string, to no avail. I've been tinkering with just this method intermittently for two weeks now and I'm at my wit's end. Could a kind soul at least provide me with some hints or point to the right direction? I change the code as var result = await HttpClient.GetStringAsync(url); and the code builds. How do I proceed then? What's the right method to call this function so it displays the right strings instead of System.Task.Task`f or the like? Thanks a million.
13 Replies
You do not convert Task<T> to T. You await the task to get T
So your method must also be async
And oh boy I see
dynamic
.
You sure there isn't a better way? Given that the api is returning JSON, you probably want to use System.Text.Json and deserialize to a C# model
Or you can use newtonsoft json instead
Avoid dynamic. You shouldn't be using it, unless you are dealing with COMCheck this out for more async await https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/async-scenarios
Asynchronous programming scenarios - C#
Learn about the C# language-level asynchronous programming model provided by .NET Core.
I will look into all of the things mentioned, thank you very much!
Are you sure the access violation isn't because of attempting to access a member of a dynamic object, which is not there?
*disclaimer, I do not know this exception by heart
it's GdiPlus.dll exception, access violation at reading address 0x0000000000
What in the world
And now we pray
Gdi stuff is generally UI related
To be perfectly honest, i get the same error while trying to retrieve the process name that launched the application, but that's another story
And you'd usually see it in a winform app
this is a winform app
š
Ahaaa
There might be more to your issue than meets the eye
probably
i'll do some reading
apparently no reading documentation is enough
thanks a million!
I recommend going with wpf
You won't get any obscure gdi errors
Proper wpf will require some mvvm knowledge, but you can survive without it for the time being
Are you creating UI components based on the loaded data?