none
Winform Async RRS feed

  • Question

  • My task is to get a number from a service.

    I made this console application:

    ImportsSystem.Net.Http

        ModuleModule1

            SubMain()

            DimContestId = CLng(Getcontestid())

            MsgBox(ContestId.ToString)

        EndSub

     

        FunctionGetcontestid() AsLong

            DimreturnTask = TaskOfgetcontestidAsync(888, Now(), "Test")

            ReturnreturnTask.Result

        EndFunction

     

        AsyncFunctionTaskOfgetcontestidAsync(Uid AsInteger, ActTime AsDate, ActName AsString) AsTask(OfLong)

     

            Dimpage AsString= "http://xyz.ab/getcontestid"

     

            DimformContent = NewFormUrlEncodedContent(

                                {

                                MakeB64Kvp("uid", Uid.ToString("0000")),

                                MakeB64Kvp("cid", ""),

                                MakeB64Kvp("date", ActTime.ToShortDateString),

                                MakeB64Kvp("name", ActName),

                                MakeB64Kvp("version", "version, format: ver_2_0")

                                }

                            )

     

            Dimclient AsSystem.Net.Http.HttpClient = NewSystem.Net.Http.HttpClient()

     

            Dimresponse = Awaitclient.PostAsync(page, formContent)

     

            Ifresponse.IsSuccessStatusCode Then

                DimrespContent = response.Content.ReadAsStringAsync

                ReturnCLng(respContent.Result)

            Else

                ReturnNothing

            EndIf

     

        EndFunction

        PrivateFunctionMakeB64Kvp(Key AsString, Value AsString) AsKeyValuePair(OfString, String)

            DimValueB64 = Convert.ToBase64String(NewSystem.Text.ASCIIEncoding().GetBytes(Value))

            ReturnNewKeyValuePair(OfString, String)(Key, ValueB64)

        EndFunction

    EndModule

    It works and the MsgBox shows the number.

    I added the three functions from this module to a WinForm application.  When the Getcontestid is called, the program hangs. I break the program. It hangs on:

    returnTask.Result

    line in Getcontestid function.  The "returntask" now contains:

    Id = 3, Status = WaitingForActivation {1}, Method = "{null}", Result = "{Not yet computed}"

        AsyncState: Nothing

        CancellationPending: False

        CreationOptions: None {0}

        Exception: Nothing

        Id: 3

        Result: 0

        Status: WaitingForActivation {1}

     

     

    Where do I do wrong?

    How can my program get this ContestId?

    Tuesday, May 28, 2019 6:13 AM

Answers

  • Hello,

    Try adding ConfigureAwait(False) to your call.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by hjl1 Tuesday, May 28, 2019 12:31 PM
    Tuesday, May 28, 2019 9:44 AM
    Moderator

All replies

  • Hi,
    your async function TaskOfgetcontestidAsync is waiting: 

    Dim response = Await client.PostAsync(page, formContent)

    In this case the 

    Return returnTask.Result

    wait for end of client.PostAsync. Please check why the PostAsync hang.


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Tuesday, May 28, 2019 7:01 AM
  • Thanks for your replay,

    If I call the function from the consol application I get quick answers, but in Windows form it hangs. 

    There is no other work.


    How can I wait?  May I block myself?



    Tuesday, May 28, 2019 9:25 AM
  • Hello,

    Try adding ConfigureAwait(False) to your call.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by hjl1 Tuesday, May 28, 2019 12:31 PM
    Tuesday, May 28, 2019 9:44 AM
    Moderator