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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:50:30+00:00 2026-06-17T17:50:30+00:00

I am trying to use async and await and my problem is that it

  • 0

I am trying to use async and await and my problem is that it wont wait for the background process to finish. Maybe you are wondering why don’t I just run the application synchronously?
I’m trying finish a part of my task ASAP and the rest can be awaited as shown in this example.
Thank you for the help guys! =)

class Program
{
    static void Main(string[] args)
    {
        Run();
        //Problem or Maybe Not? Needs this 
        //So that my application won't close immediately
        Console.ReadLine();
    }

    private async static void Run()
    {
        Task<bool> TBool = ProcessRecords();

        if (await TBool)
        {
            //Problem #1 This Line Doesn't Show
            Console.WriteLine("End of Code");
            //SHould be safe to close the application by Now
            //But goes through here not waiting for the return
            //of the last process.

            Environment.Exit(0);

            //My temporary solution is to indicate a Task.Delay(80000)
            //to make sure that all the logging in the background
            //are all done. I dont know if there is a function that shows
            //that there are task running on the background or any 
            //other workaroung will help. =) thanks
        }

    }

    private async static Task<bool> ProcessRecords()
    {
        Task newTask = null;
        //Loop through all the records and send
        //all the records to MQ ASAP
        for (int x = 0; x < 10; x++)
        {
            //I wont wait for this Task so
            //I can send the next record
            newTask = SendToMQ(x);
        }
        //I only need to wait for the last Task to
        //indicate that I can exit the system
        //as soon as it finish
        //Problem #2 The Console.WriteLine doesnt show the last record.
        await newTask;
        return true;
    }

    private async static Task SendToMQ(int count)
    {
        //actual sending of message (Important)
        await Task.Delay(1000);
        //Process of Logging Connect to DB etc, (Not so Important, but takes most of the time)
        await LoggingRecord();
        Console.Clear();
        Console.WriteLine("Done Processing  " + count.ToString() + " records");
    }

    //Logging of each record
    private async static Task LoggingRecord()
    {
        await Task.Delay(5000);
        //Problem #3 This Line Doesn't Show
        Console.WriteLine("Last Log Finished");
    }
}
  • 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-17T17:50:31+00:00Added an answer on June 17, 2026 at 5:50 pm

    You should use await whenever possible:

    await Run();
    

    However, in this case you cannot, so you must use Wait

    static void Main(string[] args)
    {
        Run().Wait();
    
        //Problem or Maybe Not? Needs this 
        //So that my application won't close immediately
        //Console.ReadLine();
    }
    
    // note the return type is Task
    private async static Task Run()
    {
    ...
    }
    

    In ProcessRecords() you have the following line – I am not quite sure what you mean, so I didn’t address it:

    //Problem #2 The Console.WriteLine doesnt show the last record.
    

    The above prints out

    Done Processing  9 records
    End of Code
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use async and request module together but i don't understand how
I'm trying to use async/await commands in Mono with a .NET profile of 4.5.
I am trying to use the 'async' and 'await' keywords in VS C# 2010
I'm trying to use async and await keywords for the first time and I
I'm trying to use the new async/await feature to asynchronously work with a DB.
I'm trying to use the FtpWebRequest async calls ( BeginGetResponse / EndGetResponse ). However,
I am trying to use the code from Microsoft for an Async Socket connection.
I'm trying to use the library jmathai/twitter-async to send tweets, but I get an
Using the new async/await model it's fairly straightforward to generate a Task that is
I'm trying to use async from the asp.net mvc futures, using my own async

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.