延遲直到 await 回傳 True
我看過許多延遲 for while 回圈直到獲得結果的例子;但是如果沒有獲得布爾變數,則無法提出不會永遠掛起系統的解決方案。
我使用過 Thread.Sleep 但它阻止等待執行緒回傳結果。我需要有人幫助我找到解決方案,系統將等待“LoadedShopifyProduct”為真,然后繼續“Button1.click”。
*** 你會看到 try 變數在達到 40 時停止,所以我真的不知道產品是否已經加載。有比 Thread.Sleep 更好的選擇嗎?
Private Sub TEST
Show()
TopMost = True
Dim HasInternet As Boolean = crm.HaveInternetConnection() :'check if there is internet
If HasInternet = False Then
Me.Close()
Exit sub
End If
Form1.LoadShopifyProducts():'calls the Async routine
Dim trys As Short = 0
While LoadedShopifyProducts = False And HasInternet = True And trys < 40
Form1.ToolStripStatusLabel1.Text = "Tries : " & trys & " -> Shopify Products Not loaded ! Please wait a few seconds"
Application.DoEvents()
Thread.Sleep(2000)
trys = trys 1
End While
Button1.PerformClick() : 'do next
End Sub
Public Async Sub LoadShopifyProducts()
Dim HasInternet As Boolean = crm.HaveInternetConnection()
If HasInternet = True Then
TimesShopifyLoad = TimesShopifyLoad 1
Dim BaseURL = Para(Val(TermNumber), 580)
Dim Key = Para(Val(TermNumber), 581)
Dim shopifyClient As New ShopifyApi(BaseURL, Key)
shopifyClient.Initialise()
'retrieve all Products from Shopify API
allShopifyProducts = Await shopifyClient.GetAllProducts()
'also collate a master list of all Variants from the list of Products returned above to allow searching by SKU which only appears on the variant
allShopifyVariants = Await shopifyClient.GetAllVariants(allShopifyProducts)
LoadedShopifyProducts = True :' after await set ---> to true ! !
CountShopify = allShopifyVariants.Count
ToolStripStatusLabel1.Text = "( Shopify : " & allShopifyVariants.Count & " ) Times:" & Str(TimesShopifyLoad)
End If
End Sub
uj5u.com熱心網友回復:
以下是你將如何做你想要的基本版本:
Private Async Sub Button1_Click(sender As Object, args As EventArgs)
If crm.HaveInternetConnection() Then
Dim variants = Await GetShopifyProducts(Para(Val(TermNumber), 580), Para(Val(TermNumber), 581))
' Do something with the variants
End If
End Sub
Private Async Function GetShopifyProducts(BaseURL As String, Key As String) As Task(Of ShopifyVariant())
Dim shopifyClient As New ShopifyApi(BaseURL, Key)
shopifyClient.Initialise()
Dim products = Await shopifyClient.GetAllProducts()
Dim variants = Await shopifyClient.GetAllVariants(products)
Return variants
End Function
如果您需要每 2 秒重復一次呼叫,直到滿足某個條件,那么它看起來像這樣:
Private Async Sub Button1_Click(sender As Object, args As EventArgs)
Me.Timer1.Interval = 2000
Me.Timer1.Enabled = True
End Sub
Private Async Sub Timer1_Tick(sender As Object, args As EventArgs)
If crm.HaveInternetConnection() Then
Dim variants = Await GetShopifyProducts(Para(Val(TermNumber), 580), Para(Val(TermNumber), 581))
' Do something with the variants
If TurnOffTimerCondition Then
Me.Timer1.Enabled = False
End If
End If
End Sub
Private Async Function GetShopifyProducts(BaseURL As String, Key As String) As Task(Of ShopifyVariant())
Dim shopifyClient As New ShopifyApi(BaseURL, Key)
shopifyClient.Initialise()
Dim products = Await shopifyClient.GetAllProducts()
Dim variants = Await shopifyClient.GetAllVariants(products)
Return variants
End Function
使用示例超時更新:
Private Async Sub Button1_Click(sender As Object, args As EventArgs)
If Me.Timer1.Enabled = False Then
Me.Timer1.Interval = 2000
Me.Timer1.Enabled = True
sw = Stopwatch.StartNew()
End If
End Sub
Private Async Sub Timer1_Tick(sender As Object, args As EventArgs)
If crm.HaveInternetConnection() Then
Dim variants = Await GetShopifyProducts(Para(Val(TermNumber), 580), Para(Val(TermNumber), 581))
' Do something with the variants
If sw.Elapsed > TimeSpan.FromSeconds(60) Then
Me.Timer1.Enabled = False
MessageBox.Show("not met in 60 seconds")
End If
End If
End Sub
uj5u.com熱心網友回復:
您可以使用后臺作業人員并從后臺作業人員中呼叫您的功能,以便在活動中作業。通過使用后臺作業人員,您可以防止系統掛斷,還可以在呼叫后臺作業人員并在完成時隱藏之前顯示一些進度條。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/503742.html
上一篇:正則運算式小數帶負
