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

  • SEARCH
  • Home
  • 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 8546047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:55:43+00:00 2026-06-11T12:55:43+00:00

Sometimes the event pattern is used to raise events in MVVM applications by or

  • 0

Sometimes the event pattern is used to raise events in MVVM applications by or a child viewmodel to send a message to its parent viewmodel in a loosely coupled way like this.

Parent ViewModel

searchWidgetViewModel.SearchRequest += (s,e) => 
{
    SearchOrders(searchWidgitViewModel.SearchCriteria);
};

SearchWidget ViewModel

public event EventHandler SearchRequest;

SearchCommand = new RelayCommand(() => {

    IsSearching = true;
    if (SearchRequest != null) 
    {
        SearchRequest(this, EventArgs.Empty);
    }
    IsSearching = false;
});

In refactoring my application for .NET4.5 I am making as much as code possible to use async and await. However the following doesn’t work (well I really wasn’t expecting it to)

 await SearchRequest(this, EventArgs.Empty);

The framework definitely does this to call event handlers such as this, but I’m not sure how it does it?

private async void button1_Click(object sender, RoutedEventArgs e)
{
   textBlock1.Text = "Click Started";
   await DoWork();
   textBlock2.Text = "Click Finished";
}

Anything I’ve found on the subject of raising events asynchrously is ancient but I can’t find something in the framework to support this.

How can I await the calling of an event but remain on the UI thread.

  • 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-06-11T12:55:45+00:00Added an answer on June 11, 2026 at 12:55 pm

    Events don’t mesh perfectly with async and await, as you’ve discovered.

    The way UIs handle async events is different than what you’re trying to do. The UI provides a SynchronizationContext to its async events, enabling them to resume on the UI thread. It does not ever "await" them.

    Best Solution (IMO)

    I think the best option is to build your own async-friendly pub/sub system, using AsyncCountdownEvent to know when all handlers have completed.

    Lesser Solution #1

    async void methods do notify their SynchronizationContext when they start and finish (by incrementing/decrementing the count of asynchronous operations). All UI SynchronizationContexts ignore these notifications, but you could build a wrapper that tracks it and returns when the count is zero.

    Here’s an example, using AsyncContext from my AsyncEx library:

    SearchCommand = new RelayCommand(() => {
      IsSearching = true;
      if (SearchRequest != null) 
      {
        AsyncContext.Run(() => SearchRequest(this, EventArgs.Empty));
      }
      IsSearching = false;
    });
    

    However, in this example the UI thread is not pumping messages while it’s in Run.

    Lesser Solution #2

    You could also make your own SynchronizationContext based on a nested Dispatcher frame that pops itself when the count of asynchronous operations reaches zero. However, you then introduce re-entrancy problems; DoEvents was left out of WPF on purpose.

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

Sidebar

Related Questions

I'm building an app which will aggregate events its users add to it. I'd
My app sometimes calls Server.Transfer in the Application_OnPostAuthenticateRequest event of the global.asax to act
Sometimes, using jQuery induces you to abuse its power (at least for me because
This delegate event is firing twice (not always, sometimes). client.bindButtonClickFunction = function(){ $(#client-plugin).delegate(.client-button, click,
Using code to hook the console close event from this thread , I sometimes
I think that you have heard of message/event buses, it's the single place when
Below is a rather frightening pattern I sometimes use as a lazy way to
Many times I work with optimized code (sometimes even involving vectorized loops), which contain
I have often seen an init() within the constructor of AS3 classes, sometimes even
Sometimes I override methods in base classes. Sometimes I even override them with an

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.