2

I tried out a few things with async/await but I dont't realy get it. All I want to achive for the beginning is to concurrently write to the Console from two different Threads. Here is the code:

static void Main(string[] args)
{
    DoSomethingAsync();

    for (int i = 0; i < 1000; i++)
        Console.Write(".");

    Console.ReadLine();
}

static async void DoSomethingAsync()
{
    Console.WriteLine("DoSomethingAsync enter");

    //This does not seem good
    await Task.Delay(1);

    for (int i = 0; i < 1000; i++)
        Console.Write("X");

    Console.WriteLine("DoSomethingAsync exit");
}

With Threads that was easy but with async/await I only get it done when I put in this strange

await Task.Delay(1);

Most basic examples I saw used this. But what to do, when you want to do something timetaking that is not async? You cant await anything and so all code runs on the main Thread. How can I achive the same behavior as with this code but without using Task.Delay()?

2
  • 5
    Don't use async void. Commented Oct 19, 2014 at 14:24
  • Have a look at my async-await curation. Commented Oct 25, 2014 at 9:26

2 Answers 2

8

Parallel and concurrent are not the same thing. If you want your for loop to be executed in parallel use Task.Run to offload that work to a different ThreadPool thread:

static void Main(string[] args)
{
    var task = Task.Run(() => DoSomething());

    for (int i = 0; i < 1000; i++)
        Console.Write(".");

    task.Wait() 
}

static void DoSomething()
{
    for (int i = 0; i < 1000; i++)
        Console.Write("X");
}

async-await is used for asynchronous operations, which may run concurrently but not necessarily.

Sign up to request clarification or add additional context in comments.

1 Comment

To elaborate on the async/await question: it's important to understand what these keywords do and what they don't do. In particular, they do not actually cause anything to run asynchronously. What they do do is tell the compiler that the async method is expected to invoke some kind of operation that will complete asynchronously, where the method wants to wait for the completion of that operation and then continue with something else. Without something to "await" in the async method, it just completes straight through like a normal method.
0

You could use an async Task method and call Wait on the resulting task. This will work. It will, however, introduce concurrency because timer ticks are served on the thread pool. I'm not sure what thread-safety guarantees the console makes.

Consider setting up a single threaded synchronization context. Such a thing behaves very much like a UI thread. All your code runs single threaded, yet you can have multiple async methods executing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.