Specified cast not valid using webbrowser control

Mattia Fanti 356 Reputation points
2022-03-27T01:20:58.677+00:00

Hi, I'm using the Telegram.bot package from nuget and getting the last message from the client. With the latter, I'm setting the attribute of a textbox on a webpage with the control WebBrowser.
The code I'm using is:

Private Sub InsertLink(link as string)

        MsgBox(link)

        WebBrowser1.Document.GetElementById("video_url").SetAttribute("value", link)
         WebBrowser1.Document.GetElementById("submitButton").InvokeMember("click")
         Catch ex As Exception
         MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim botid As String = "my bot api"
        Dim bot As New TelegramBotClient(botid)

        Dim cts = New CancellationTokenSource()
        Dim options = New ReceiverOptions With {
            .AllowedUpdates = Array.Empty(Of UpdateType)()
        }
        bot.StartReceiving(updateHandler:=AddressOf HandleUpdateAsync, errorHandler:=AddressOf HandleErrorAsync, options, cts.Token)
        Console.ReadLine()
    End Sub
    Public link As String

    Private Async Function HandleUpdateAsync(botClient As ITelegramBotClient,
                                        update As Update,
                                        cancellationToken As CancellationToken) As Task

        Try
            If update.Message IsNot Nothing Then

                link = update.Message.[text]
                InsertLink(link)


            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function

    Private Function HandleErrorAsync(botClient As ITelegramBotClient,
                                exception As Exception,
                                cancellationToken As CancellationToken) As Task
        Console.WriteLine(exception)
        Return Task.CompletedTask

    End Function
End Class

The problem comes when I need to pass the variable link to the private sub InsertLink, since HandleUpdateAsync is operating in a separated thread, so I'm getting the error Specified cast is not valid on the ex.message of the private sub. How I can I avoid in this case the cross thread and pass correctly the variable link to it? Also, would be nice if you can point me out how to correctly make await the async method in HandleUpdateAsync() - and also await the error handler, HandleErrorAsync().Many thanks

Developer technologies VB
{count} votes

4 answers

Sort by: Most helpful
  1. Jose Zero 576 Reputation points
    2022-03-28T23:15:06.21+00:00

    For Async/Await in VB check this link Asynchronous programming with Async and Await (Visual Basic)

    I did not look in details what you doing, but I think you should use Await InsertLink(link)


  2. Jose Zero 576 Reputation points
    2022-03-30T12:49:23.9+00:00

    What you getting now?
    Does "link" parameter have a value of type string?
    By the way in HandleErrorFunction use Console.WriteLine(exception.Message)


  3. Jose Zero 576 Reputation points
    2022-03-31T23:01:23.943+00:00

    Not saying that "there is a bug", but, since there is a mix of languages (C#/VB), I'm considering a possible issue (if from Nuget Pack) between languages that can be fixed on your side when you know when, what and why.

    The main point here, is, from where Exception is coming from?
    It can be from Nuget Pack, Yes.
    It can be from a Cross-Thread, Yes

    My suggestion to use Source instead of Nuget Pack might help to define if Exception is coming from Pack or not, it can create some other problems, yes it can or not!
    On the other hand, considering a Cross-Thread, check this how-to-make-thread-safe-calls-to-windows-forms-controls
    And also search for" Invalid cross-thread operation form1 control was accessed by a thread other than the one it was created from"


  4. Jose Zero 576 Reputation points
    2022-04-01T22:50:17.267+00:00

    Answering your question, read again my first words in last message. In addition, review all thread and watch gifs, perhaps you will get at same point as me.
    From my point of view there is no bullet proof package, I already found non predicted issues in some packages, sometimes proposed changes or just get a work around from creator.

    About Cross-Thread you mention on SO, if you believe it is your main issue, you can try use same process but instead of get "text" value, get the "control", so you can have all control properties.

    Here 2 links (both C#/Winforms) that might help or give you some clue about directions to take .
    First one is a bunch of 5 short videos. (yes it could be only one). Don´t waste your time getting source code. It interacts with Form Controls
    playlist
    Second one, might not fit Form Controls interation, but it is about Winforms
    watch


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.