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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:39:15+00:00 2026-06-16T04:39:15+00:00

I need to create multiple async calls like IList<Task> Ts = new List<Task>(); Ts.Add(GetInformationFromServer(ID));

  • 0

I need to create multiple async calls like

IList<Task> Ts = new List<Task>();
Ts.Add(GetInformationFromServer(ID));

But I do not what do await in the thread, I am calling from

So it should be possible to do like this (one of the old ways, but are there a new one?), from another call

GetInformation(string ID) {
   while (!Finish) {
      Thread.Sleep(100);
   }
   return _Information.First(a=>a.ID==ID);
 }

I could of course save the Tasks in a variable, but how can I start them? And how do I get the status?
I think I can await them in another thread, but how can I check if they are finish? Should I implement it on my own?
And how do I start them (should I just use Task.WhenAll without await)?

UPDATE

I figured out, I have to implement my own way, so the answer is kind of this way, but I need to use Task instead of Func

/// The executed elements
private IList<T> _ExecutedElements;

/// The Stack over the elements to be executed
private Stack<T> _ExecutingElements;

/// The method to be runned
private Func<object, Task<T>> _Method;

/// Should the while-loop start?
private bool _Running;

/// The Task
private Task<T> _Task;

/// Construct the class
/// <param name="Method">The function to be executed</param>
public MultiAsync(Func<object, T> Method) {
   _Method = Method;
}

/// Add an element
/// <param name="Item">The item to be added</param>
public void AddItem(T Element) {
   _ExecutingElements.Push(Element);
}

/// Execute the method
public async void ExecuteAsync() {

   // Set it to start running
   _Running = true;

   // While there are elements left
   while (_ExecutingElements.Count > 0) {

      // Check if it is not running, and if it isn't break out
      if (!_Running) { break; }

      // The current element
      T Element = default(T);

      // Pop out the element, that has not been runned through
      do { Element = _ExecutingElements.Pop(); }
      while (!_ExecutedElements.Contains(Element));

      // Check if there is an element, and if there is execute the method and await it
      if (Element != default(T)) {
         await ExecuteMethodAsync(Element);
      }
   }
}

/// Execute the item
/// <param name="Item">The item to be executed</param>
/// <returns>The executed item (due to reflection in FillInformation, the Item is filled)</returns>
public async Task<T> ExecuteItemAsync(T Item) {

   // Check if the item has not been executed, and if it is not executed
   if (!_ExecutedElements.Contains(Item)) {

      // Stop the while-loop
      _Running = false;

      // Check if the Task is running, and if it is await it
      if (_Task != default(Task) && !_Task.IsCompleted && !_Task.IsFaulted) {
         await _Task;
      }

      // Execute the method using the specific item
      await ExecuteMethodAsync(Item);
   }

   // Start the while-loop
   ExecuteAsync();

   // Return the element
   return Item;
}

/// Execute the method
/// <param name="Item">The item to run</param>
/// <returns>The Task to be executed</returns>
private async Task ExecuteMethodAsync(T Item) {

   // Set the Task
   _Task = _Method.Invoke(Item)

   // Start the task
   T Element = await _Task;

   // Add the item to the List
   _ExecutedElements.Add(Element);

   // Fill the information
   FillInformation(Element);
}

The call is like this

private async void FillTasksAsync(IEnumerable<Model.Task> Tasks) {
   _InfoLoader = new MultiAsync<Model.Task>(Tsk => { return GetTaskAsync(Tsk); });

   foreach (var Tsk in Tasks) {
      _InfoLoader.AddItem(Tsk);
   }
}
  • 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-16T04:39:16+00:00Added an answer on June 16, 2026 at 4:39 am

    I have a blog post that discusses asynchronous initialization, which sounds like what you need. It’s derived from an original idea by Stephen Toub.

    In this case, you could use:

    List<AsyncLazy<string>> Ts = ...
    Ts.Add(new AsyncLazy<string>(() => GetServerStringAsync(ID));
    

    To start one downloading, you can do:

    Ts[0].Start();
    

    And when you need it, you can do:

    var result = await Ts[0];
    

    which will (asynchronously) wait for it to finish downloading if it hasn’t already. If it has already, then you’ll get the result immediately.

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

Sidebar

Related Questions

I'm new to GUI programming, but need to create a multiple window GUI. Does
I`v come across a need where I want to create multiple list items from
I need to setup multiple queues on an exchange. I would like to create
I need to create multiple /testcontainer: parameters to feed into a task that exec's
I need to create a drop down list that contain multiple checkboxes as well
In Silverlight app I need to run multiple async calls in parallel - they
I have a need to create multiple processing threads in a new application. Each
Why do I need to create Multiple SSPs in MOSS? My manager (sharepoint administrator)
I need to create a WP site which will have multiple subjects. Each subject
I need to create an ASN.1 BER encoded file with multiple records. I've been

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.