如何在Silverlight for Windows Phone 8.1中等待异步事件返回

编程入门 行业动态 更新时间:2024-10-10 06:18:37
本文介绍了如何在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中等待异步事件返回

本文发布于:2023-11-23 23:12:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623127.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   如何在   Silverlight   Windows   Phone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!