延迟,直到等待返回True



延迟直到等待返回True

我看过许多延迟一个while循环直到得到结果的例子;但如果没有达到布尔变量,就无法想出一个不会永远挂起系统的解决方案。

我使用了Thread.Sleep,但它阻止等待线程返回结果。我需要有人帮助我找到一个解决方案,其中系统将等待";LoadedShopifyProduct;若为真,则继续使用";按钮1.点击";。

***你会看到,一旦达到40,try变量就会停止,所以我真的不知道产品是否已经加载。有比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

以下是如何实现您想要的基本版本:

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

您可以使用后台工作人员并从后台调用函数。工作人员在事件中执行工作。通过使用后台工作程序,您可以防止系统挂起,还可以在调用后台工作程序并在完成时隐藏之前显示一些进度条。

最新更新