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 8879469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:57:25+00:00 2026-06-14T19:57:25+00:00

I am toying around with an async service for a Windows 8 project and

  • 0

I am toying around with an async service for a Windows 8 project and there are some async calls of this service, which should only be called once at a time.

 public async Task CallThisOnlyOnce()
 {
      PropagateSomeEvents();

      await SomeOtherMethod();

      PropagateDifferentEvents();
 }

Since you cannot encapsulate an async call in a lock statement, i thought of using the AsyncLock pattern, but than i thought i might as well try something like this:

 private Task _callThisOnlyOnce;
 public Task CallThisOnlyOnce()
 {
      if(_callThisOnlyOnce != null && _callThisOnlyOnce.IsCompleted)
         _callThisOnlyOnce = null;

      if(_callThisOnlyOnce == null)
         _callThisOnlyOnce = CallThisOnlyOnceAsync();

      return _callThisOnlyOnce;
 }

 private async Task CallThisOnlyOnceAsync()
 {
      PropagateSomeEvents();

      await SomeOtherMethod();

      PropagateDifferentEvents();
 }

Therefore you would end up with the call CallThisOnlyOnceAsync only executed once simultanously, and multiple awaiters hooked on the same Task.

Is this a “valid” way of doing this or are there some drawbacks to this approach?

  • 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-14T19:57:26+00:00Added an answer on June 14, 2026 at 7:57 pm

    A task can have multiple awaiters. However, as Damien pointed out, there’s serious race conditions with your proposed code.

    If you want the code executed each time your method is called (but not simultaneously), then use AsyncLock. If you want the code executed only once, then use AsyncLazy.

    Your proposed solution attempts to combine multiple calls, executing the code again if it is not already running. This is more tricky, and the solution heavily depends on the exact semantics you need. Here’s one option:

    private AsyncLock mutex = new AsyncLock();
    private Task executing;
    
    public async Task CallThisOnlyOnceAsync()
    {
      Task action = null;
      using (await mutex.LockAsync())
      {
        if (executing == null)
          executing = DoCallThisOnlyOnceAsync();
        action = executing;
      }
    
      await action;
    }
    
    private async Task DoCallThisOnlyOnceAsync()
    {
      PropagateSomeEvents();
    
      await SomeOtherMethod();
    
      PropagateDifferentEvents();
    
      using (await mutex.LockAsync())
      {
        executing = null;
      }
    }
    

    It’s also possible to do this with Interlocked, but that code gets ugly.

    P.S. I have AsyncLock, AsyncLazy, and other async-ready primitives in my AsyncEx library.

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

Sidebar

Related Questions

I'm toying around with GWT (dunno if this matters) and Hibernate. I've created a
In goofing around with some F# (via MonoDevelop), I have written a routine which
I'm toying around with jade.js and Bootstrap, from twitter. Bootstrap has some classes with
I'm trying to download a JSON string in my Windows Store App which should
trying to wrap my head around some basic async programming concepts. Right now I
Hi I am trying to play around with async.parallel and here is my code
I'm toying around with the Facebook APIs for a game I'm developing as a
Right now I am toying around with an idea in my head, and have
I've been toying around with switching from ms-access files to SQLite files for my
I'm currently toying around with the Clarity .NET Facebook API but am finding certain

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.