Home > Enterprise >  Calling an async function in Task.Run
Calling an async function in Task.Run

Time:09-30

I use a waiting form with an animated gif inside. The only way i've found to display this form with the animated gif "moving" is :

frmWaiting.Show()
Await Task.Run(Sub() AnyKindOfActionInASub)
frmWaiting.Close()

It works well but what if i need to call an async function returning an object instead of a simple sub? If i do :

Dim TwitterAccountToAdd As TwitterAccount
frmWaiting.Show()
TwitterAccountToAdd = Await nsTweetInvi.CallTwitterApi.GetApiTwitterDatasAccount(Me.txtTwitterId.Text)
frmWaiting.Close()

The animated gif is replaced by a white square.

Considering that GetApiTwitterDatasAccount return a Task(Of TwitterAccount), how can i do this?

CodePudding user response:

Following Jimi advices, i found a solution that works perfectly.

Private TwitterAccountToAdd As New TwitterAccount
Dim strTwitterId As String = Me.txtTwitterId.Text
Dim frmWaitingTwitter As New frmWaiting
Me.TopMost = False
With frmWaitingTwitter
     .TopMost = True
     .Show()
     .StartPosition = FormStartPosition.CenterScreen
End With
TwitterAccountToAdd = Await Task.Run(Function() CheckAndGetTwitterAccount(strTwitterId))
frmWaitingTwitter.Close()
Me.TopMost = True
  • Related