在winform中运行Async / Await代码时,它阻止Winform UI,但在控制台模式下运行时有效

时间:2018-05-21 16:46:15

标签: vb.net async-await synchronization

我创建了一段代码,从控制台应用程序运行时效果很好,而当项目是winform应用程序时则不行。下面是代码,它使用了一个库,你可以从名为bandwidth 3.1.5的'nuget'中获取,这个库允许你使用该库调用其他API发送短信测试消息等。我创建了这个函数异步运行希望修复问题当从同步模式运行时,从winform应用程序运行它锁定似乎它在运行异步时执行相同或我只是没有做正确的事情?

''' <summary>
''' Gets a list of your numbers.
''' </summary>
''' <param name="size">Optional: Used for pagination to indicate the size of each page requested for querying a
''' list of phone numbers. If no value is specified the default value is 25. (Maximum value 1000)</param>
''' <param name="name">Optional: Used to filter the retrieved list of numbers allocated for the authenticated user by it’s name.</param>
''' <param name="applicationId">Optional: Used to filter the retrieved list of numbers by an associated application ID.</param>
''' <param name="state">Optional: Used to filter the retrieved list of numbers allocated for the authenticated user by a US state.</param>
''' <param name="city">Optional: Used to filter the retrieved list of numbers allocated for the authenticated user by it’s city.</param>
''' <param name="numberState">Optional: Used to filter the retrieved list of numbers allocated for
''' the authenticated user by the number state. <seealso cref="Bandwidth.Net.Api.PhoneNumberState"/></param>
''' <returns></returns>
Public Shared Async Function GetListOfPhoneNumbers(ByVal size As Integer?, ByVal name As String, ByVal applicationId As String,
    ByVal state As String, ByVal city As String, ByVal numberState As Api.PhoneNumberState?) As Threading.Tasks.Task(Of List(Of Api.PhoneNumber))

    Dim objClient As New Client(CommonApplication.BandWidth_USER_ID,
        CommonApplication.Bandwidth_API_TOKEN, CommonApplication.Bandwidth_API_SECRET)

    Dim objQuery As New Api.PhoneNumberQuery With
        {
            .Size = size,
            .Name = name,
            .ApplicationId = applicationId,
            .State = state,
            .City = city,
            .NumberState = numberState
        }

    Dim objResponse = Await System.Threading.Tasks.Task.Run(
        Function()
            Dim response = objClient.PhoneNumber.List(objQuery)

            Return response.ToList()
        End Function)

    Return objResponse
End Function

1 个答案:

答案 0 :(得分:0)

好吧,我想出来,如果其他人遇到同样的问题,你可以通过看看来了解在winform中使用async / await:

Stephen Cleary的

Async and Await