本文介绍了如何在Silverlight for Windows Phone 8.1中等待异步事件返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下代码段中,我无法等待已完成的事件触发。 WaitOne似乎永远等待。我需要知道在VB.Net中这样做的正确方法。干杯。
In the following code snippet I am having difficulty waiting for the completed event to fire. The WaitOne seems to just wait forever. I need to know the proper way of doing this in VB.Net. Cheers.
Private mTable As agDBConnect.Table Private autoResetEvent As New System.Threading.AutoResetEvent(False) Public Sub GetTableCompleted(sender As Object, e As agDBConnect.GetTableCompletedEventArgs) Try Me.Dispatcher.BeginInvoke(New Action(Of agDBConnect.Table)(AddressOf FillTable), e.Result) autoResetEvent.Set() Catch ex As Exception End Try End Sub Private Sub FillTable(aTab As agDBConnect.Table) mTable = aTab End Sub Private Sub DoGetTable() AddHandler myagDBConnect.GetTableCompleted, AddressOf GetTableCompleted While myagDBConnect.State <> ServiceModel.CommunicationState.Opened End While myagDBConnect.GetTableAsync("SELECT * from s3USER") autoResetEvent.WaitOne() End Sub Private Async Sub btTestData_Click(sender As Object, e As RoutedEventArgs) Handles btTestData.Click Try Try mTable = Nothing Dim myThread As System.Threading.Thread = New Thread(AddressOf DoGetTable) myThread.Start() While myThread.ThreadState = ThreadState.Running End While '>> don't want to be here until gettablecomplete has done If mTable IsNot Nothing AndAlso mTable.Rows.Count > 0 Then txtDebug.Text = mTable.Rows(0).Columns(0).FieldName End If Catch ex As Exception End Try Catch ex As Exception MessageBox.Show(ex.ToString, "System Error", MessageBoxButton.OK) End Try End Sub推荐答案
你不需要创建新的线程(除非你知道你在做什么,否则你不应该这样做);也不需要AutoResetEvent并调用Dispatcher.BeginInvoke。 只需使用 Async with Await 如何使用它。 您需要等待异步点击处理程序中的GetTableAsync方法。 查看此文章 msdn.microsoft/en-us/library/hh191443.aspx [ ^ ]。
更多推荐
如何在Silverlight for Windows Phone 8.1中等待异步事件返回
发布评论