Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6148477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:17:19+00:00 2026-05-23T19:17:19+00:00

Relevant to Silverlight 5 / Async CTP I want to create an asynchronous function

  • 0

Relevant to Silverlight 5 / Async CTP

I want to create an asynchronous function that initiates a layout update and then Awaits for the layout update to complete. Something like:

    Private Async Function UpdateLayoutRoot() As Task
       LayoutRoot.UpdateLayout()
       Await LayoutRoot.LayoutUpdated  <--- (NOT valid but shows desired outcome)           
    End Function

How can this be done? More generally, how can you use Await to wait for existing events?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:17:20+00:00Added an answer on May 23, 2026 at 7:17 pm

    Thanks Cory! Your suggestion to use TaskCompletionSource is just what I needed. I’ve combined the use of a TaskCompletionSource with the Lucian Wischik’s Async CTP specification to develop a pair of generic Silverlight 5 classes that can be used to Await any CLR, or routed event. Only the Async CTP (AsyncCtpLibrary_Silverlight) is required (The formidable Rx library is not needed). Here are the two classes:

    Public Class AwaitableEvent(Of TResult)
    
        Private eta As EventTaskAwaiter(Of TResult) = Nothing
    
        Sub New(ByVal Sender As Object, ByVal EventName As String)
            eta = New EventTaskAwaiter(Of TResult)
            Dim ei as EventInfo = Sender.GetType.GetEvent(EventName)
            Dim d = [Delegate].CreateDelegate(ei.EventHandlerType, 
                Me, "EventCompletedHandler", True, True)
            ei.AddEventHandler(Sender, d)
        End Sub
    
        Public Function GetAwaiter() As EventTaskAwaiter(Of TResult)
            Return eta
        End Function
    
        Private Sub EventCompletedHandler(ByVal sender As Object, ByVal e As TResult)
            eta.tcs.TrySetResult(e)
        End Sub
    
    End Class
    
    Public Class EventTaskAwaiter(Of TResult)
    
        Friend tcs As New TaskCompletionSource(Of TResult)
    
        Public ReadOnly Property IsCompleted As Boolean
            Get
                Return tcs.Task.IsCompleted
            End Get
        End Property
    
        Sub OnCompleted(r As Action)
            Dim sc = SynchronizationContext.Current
            If sc Is Nothing Then
                tcs.Task.ContinueWith(Sub() r())
            Else
                tcs.Task.ContinueWith(Sub() sc.Post(Sub() r(), Nothing))
            End If
        End Sub
    
        Function GetResult() As TResult
            If tcs.Task.IsCanceled Then Throw New TaskCanceledException(tcs.Task)
            If tcs.Task.IsFaulted Then Throw tcs.Task.Exception.InnerException
            Return tcs.Task.Result
        End Function
    
    End Class
    

    Here’s an example of using the AwaitableEvent class to Await mouse, keyboard and timer events.

    Private Sub AECaller()
        GetMouseButtonAsync()
        MessageBox.Show("After Await mouse button event")
        GetKeyAsync()
        MessageBox.Show("After Await key event")
        GetTimerAsync()
        MessageBox.Show("After Await timer")
    End Sub
    
    Private Async Sub GetMouseButtonAsync()
        Dim ae As New AwaitableEvent(Of MouseButtonEventArgs)(LayoutRoot, "MouseLeftButtonDown")
        Dim e = Await ae
        MessageBox.Show(String.Format("Clicked {0} at {1},{2}",
                                      e.OriginalSource.ToString,
                                      e.GetPosition(LayoutRoot).X,
                                      e.GetPosition(LayoutRoot).Y))
    End Sub
    
    Private Async Sub GetKeyAsync()
        Dim ae As New AwaitableEvent(Of KeyEventArgs)(LayoutRoot, "KeyDown")
        Dim e = Await ae
        MessageBox.Show(String.Format("Key {0} was pressed", e.Key.ToString))
    End Sub
    
    Private Async Sub GetTimerAsync()
        Dim StopWatch As New DispatcherTimer
        StopWatch.Interval = New TimeSpan(TimeSpan.TicksPerSecond * 6)
        Dim ae As New AwaitableEvent(Of EventArgs)(StopWatch, "Tick")
        StopWatch.Start()
        Await ae
        MessageBox.Show(String.Format("It's {0}seconds later!", StopWatch.Interval.TotalSeconds))
        StopWatch.Stop()
    End Sub
    

    As expected the Await statement returns control to the calling function immediately. When the events are subsequently completed, Await assigns the result (the event args appropriate for the event being monitored) and the remaining code in the asynchronous method is then run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small audio application (in Silverlight, but that's not really relevant I
I'm trying to implement drag and drop in Silverlight using F# and asynchronous workflows.
FYI I'm pretty new to Silverlight. Okay, so I want to build a simple
I want to use MSBuild to grab and create the relevent elements for 2
This question is no longer relevant now that microsoft have released a proper version
I'll preface this and say that I'm new to Silverlight development by about week
I've got this class being provided by a web service that is then being
I have a silverlight 4 childwindow that has a textbox. When I click existing
Using VS2008, Silverlight 4. I have created an AttachedProperty, RequiresRole, that I'm setting in
relevant code: function stepNetwork() { for(i = 0; i<maxNeurons; i++) { if(neuronArray[i].charge >= neuronArray[i].threshhold)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.