Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nothing is happening #15

Open
grandeljay opened this issue Jun 3, 2022 · 15 comments
Open

Nothing is happening #15

grandeljay opened this issue Jun 3, 2022 · 15 comments

Comments

@grandeljay
Copy link

grandeljay commented Jun 3, 2022

I am new to asynchronous programming in .net so I'm hoping that I am making an obvious mistake here. Nothing is happening when I Await Translator

Dim AuthKey As String = "01234567-89ab-cdef-0123-456789abcdef:fx" ' Free API Key
Dim Translator As New Translator(AuthKey)
Dim Options As New TextTranslateOptions With {
    .Formality = Formality.More,
    .TagHandling = "html",
    .SentenceSplittingMode = SentenceSplittingMode.NoNewlines
}

Dim TranslatedText = Await Translator.TranslateTextAsync(
    TextToTranslate,
    FormMain.Settings.LanguageSource.DeepLLanguageCode,
    FormMain.Settings.LanguageTarget.DeepLLanguageCode,
    Options
)

EDIT: To clarify, I literally mean nothing, there is no error and the code also doesn't continue to execute. It just waits forever after that call.

Please advise. Thanks!
- Jay

@daniel-jones-deepl
Copy link
Member

Hi Jay, thanks for creating this issue.

It seems like your code is correct. Do you have this code placed within an Async function?

To rule out other problems, you could try another async function known to work, for example System.Io.File.WriteAllTextAsync (docs here).

If that works as expected, there could be a problem with your internet connection. This .NET library retries failed requests automatically, so to get a failure faster you could try lowering the number of retries as explained here.

@grandeljay
Copy link
Author

Hey Daniel,

the file is created, I am just very confused about the debugger not continuing.

Here are some details about the code I am using:

Public Shared Function GetTranslationForPO(TextToTranslate As String, Optional Context As String = Nothing) As String
    Return GetTranslationForPOAsync(TextToTranslate, Context).Result
End Function

Private Shared Async Function GetTranslationForPOAsync(TextToTranslate As String, Context As String) As Task(Of String)
    ' [truncated version]

    Dim AuthKey As String = "01234567-89ab-cdef-0123-456789abcdef:fx" ' Free API Key
    Dim Translator As New Translator(AuthKey)
    Dim Options As New TextTranslateOptions With {
        .Formality = Formality.More,
        .TagHandling = "html",
        .SentenceSplittingMode = SentenceSplittingMode.NoNewlines
    }

    ' Debugging seems to stop after this step
    Await IO.File.WriteAllTextAsync("right-here.txt", "This file is created successfully.")    

    ' I am expecting this to be the next step
    Dim TranslatedText = Await Translator.TranslateTextAsync(
        TextToTranslate,
        FormMain.Settings.LanguageSource.DeepLLanguageCode,
        FormMain.Settings.LanguageTarget.DeepLLanguageCode,
        Options
    )

    ' I never get here while debugging
    Return TextToTranslate
End Function

Would love some more insight! Thanks.

@daniel-jones-deepl
Copy link
Member

Okay, then if you get the same debugging problem with a system function, it might be something unrelated to the DeepL library.

I am not very familiar with Visual Basic code, but it seems like in this StackOverflow thread someone has a similar problem. The answer from Stephen Cleary suggests your problem could be that your program is exiting before the async functions complete? Although you did say the program waits, rather than that it stops. Calling .Result is also equivalent to a .Wait() call, so I am not sure.

Another idea: do you step through the code line-by-line while debugging, or do you set breakpoints? Breakpoints may work better, because some debuggers lose the execution line with async code.

@grandeljay
Copy link
Author

do you step through the code line-by-line while debugging, or do you set breakpoints?

I've tried both with the same result. I am familiar with async programming in general (i. e. JavaScript) but in vb.net it's just confusing for me, I really don't understand this behaviour, it doesn't make any sense to me. 😞

The answer from Stephen Cleary suggests your problem could be that your program is exiting before the async functions complete?

My program is not exiting, it's still running and seems to just sit still and Await for eternity.

I've spent so much time the last days trying to find something online but no luck yet 😖

@daniel-jones-deepl
Copy link
Member

daniel-jones-deepl commented Jun 10, 2022

Hi @grandeljay, I looked into this but I cannot reproduce what you saw, using JetBrains Rider. In debug mode I am also able to step through this code without issue.
Here is the code I used (in a file Program.vb):

Imports System
Imports DeepL

Module Program
    Sub Main(args As String())
        Console.WriteLine(GetTranslationForPO("Hello"))
        MainAsync(args).Wait()
    End Sub

    Private Async Function MainAsync(args As String()) As Task
        Dim result = Await GetTranslationForPOAsync("Hello")
        Console.WriteLine(result)
    End Function

    Public Function GetTranslationForPO(TextToTranslate As String) As String
        Return GetTranslationForPOAsync(TextToTranslate).Result
    End Function

    Private Async Function GetTranslationForPOAsync(TextToTranslate As String) As Task(Of String)
        Dim AuthKey As String = "01234567-89ab-cdef-0123-456789abcdef:fx" ' Free API Key
        Dim Translator As New Translator(AuthKey)
        
        Dim Options As New TextTranslateOptions With {
                .Formality = Formality.More,
                .TagHandling = "html",
                .SentenceSplittingMode = SentenceSplittingMode.NoNewlines
                }

        Dim TranslatedText = Await Translator.TranslateTextAsync(TextToTranslate, "en", "de", Options)
        Return TranslatedText.Text
    End Function
End Module

The expected output is "Hallo" twice (Hello in German).
If you do not get that output with the code above, could you give more details about your OS, IDE, and platform?

@AdrianEfford
Copy link

AdrianEfford commented Jun 14, 2022

Hi together (@daniel-jones-deepl , @grandeljay)
I'm having the same issue in Visual Studio 2022 (C#)... After trying to figure out what the issue exactly is, I got following exception:
image

The Assembly 'Polly, Version=7.0.0.0' was not found
Any ideas on what might solve this problem? Thank you in advance

@grandeljay
Copy link
Author

@daniel-jones-deepl, thank you for all your efforts so far!

I looked into this but I cannot reproduce what you saw, using JetBrains Rider. In debug mode I am also able to step through this code without issue.
Here is the code I used (in a file Program.vb):

It's the same behaviour for me, nothing is happening. Something seems to be wrong with my IDE or I am totally not understanding how to use it.

If you do not get that output with the code above, could you give more details about your OS, IDE, and platform?

OS: Windows 10
IDE: Microsoft Visual Studio Community 2022 (64-bit)
Platform: VB.NET

@daniel-jones-deepl
Copy link
Member

Hi @grandeljay, I just tested the Program.vb code above in Microsoft Visual Studio Community 2022 (64-bit) Version 17.2.4, on Windows 10, and unfortunately I get the expected output, so I can't reproduce your issue. Debugging also works as expected.
I realized that my "platform" question was a bit vague, "Framework" is probably the right term. The Framework I tested with is .NET 6.0.
I'm not sure if there is much else I can help with, there seems to be a problem with your system.

@daniel-jones-deepl
Copy link
Member

Hi @AdrianEfford,
Polly version 7.0.0.0 is a dependency of this DeepL library; normally it should be installed automatically when you install DeepL.NET. You could try installing Polly manually.

@grandeljay
Copy link
Author

grandeljay commented Jul 11, 2022

Thanks for all the suggestions!

I have tried resetting & reinstalling my IDE (Visual Studio Community 2022) and installing Polly but unfortunately nothing works. In the meantime I will be using the unofficial DeepL library, as this works without any problems for me. It also seems to use async functions but they are executing fine for me. I am really starting to think something is wrong with this library rather than my machine.

I am sorry I could not give you enough information to reproduce my issue, please let me know if I can ever try/test something for you, as I would much prefer to use the official DeepL library.

@gszdev
Copy link

gszdev commented Aug 2, 2022

Hi @grandeljay,
I think you are faced with a deadlock and not an issue of deepl-dotnet library. I don't know if your are running a GUI based app. But if you using WinForms, WPF or ASP.NET MVC, ... you have a UI-Thread and which is internally working with a SynchronizationContext. Here are some links with infos to solve your issue:

C# Deadlocks in Depth – Part 2
ConfigureAwait FAQ

Maybe you can started with the following sample replacing the [Do] part:

Private Sub OnButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim x As Integer
    Task.Run(Function()
                 x = [Do]().Result
             End Function).Wait()
End Sub

P.S: Here's a link to a C# => VB.NET converter Telerik Code Converter

@dmki
Copy link

dmki commented Oct 12, 2023

Same thing in C# VS 2022. Unofficial lib works fine. Ugly hack would be to do something like this:
TextResult[] result = null;
Task.Run(() =>
{
result = translator.TranslateTextAsync(texts, sourceLang, targetLang, translateOptions).Result;
}).Wait();

(where translator is a configured instance of Translator.

@grandeljay
Copy link
Author

grandeljay commented Nov 19, 2024

It's been a couple of years now and I've tried it again on the same machine but a new project. I'm attempting the synchronous way:

Dim Translator As New DeepL.Translator("xxxxx")
Dim Options As New TextTranslateOptions With {
    .Formality = Formality.PreferMore
}

Dim Task As Task(Of TextResult) = Translator.TranslateTextAsync(
    GermanTranslation.GetValue,
    LanguageCode.German,
    LanguageCode.French,
    Options
)
Dim TranslatedText As String = Task.Result.Text

It's the same issue for me still: nothing happens when I call Task.Result.Text.

Could you please let me know if you can find a mistake in my approach?

@grandeljay
Copy link
Author

grandeljay commented Nov 19, 2024

I'm using this code within a BackgroundWorker. Could the issue lie there? Still the same confusing behaviour.

@grandeljay
Copy link
Author

Sorry for all the spam but I totally missed @gszdev's reply, it fixes the issue for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants