Skip to main content
added 282 characters in body
Source Link
Tomas Chabada
  • 3k
  • 1
  • 20
  • 19

As you stated, the output indicates the synchronous processing, because it actually is a synchronous processing.

Defining Task does not mean asynchronous processing. There is no new Task creation, you only wrapped code to Task, but you did not create a new one.
If you want to test async processing, you should modify your method:

private Task<int> TestAsync()
{
    return Task.Run(() =>
    {
        for (int i = 0; i < 20000; i++)
            ;
        Console.WriteLine("Logic C");
        return Task.FromResult(0);
    });
}

To answer your another question:

You don't have desired output because your Main method is not waiting for asynchronous operation to finish. So as a execution in method TestAll goes to int value = await y; the main method will continue and will finish gracefully.

Update your Main method to wait for async task and you will get desired output:

static void Main(string[] args)
{
   TestAll().Wait();
}

But look at synchronously waiting on asynchronous task problem. It is related to context threads (UI thread, ASP context thread,...) so it should be safe with Console app, but always good to know what is behind.

As you stated, the output indicates the synchronous processing, because it actually is a synchronous processing.

Defining Task does not mean asynchronous processing. There is no new Task creation, you only wrapped code to Task, but you did not create a new one.
If you want to test async processing, you should modify your method:

private Task<int> TestAsync()
{
    return Task.Run(() =>
    {
        for (int i = 0; i < 20000; i++)
            ;
        Console.WriteLine("Logic C");
        return Task.FromResult(0);
    });
}

To answer your another question:

You don't have desired output because your Main method is not waiting for asynchronous operation to finish. So as a execution in method TestAll goes to int value = await y; the main method will continue and will finish gracefully.

As you stated, the output indicates the synchronous processing, because it actually is a synchronous processing.

Defining Task does not mean asynchronous processing. There is no new Task creation, you only wrapped code to Task, but you did not create a new one.
If you want to test async processing, you should modify your method:

private Task<int> TestAsync()
{
    return Task.Run(() =>
    {
        for (int i = 0; i < 20000; i++)
            ;
        Console.WriteLine("Logic C");
        return Task.FromResult(0);
    });
}

To answer your another question:

You don't have desired output because your Main method is not waiting for asynchronous operation to finish. So as a execution in method TestAll goes to int value = await y; the main method will continue and will finish gracefully.

Update your Main method to wait for async task and you will get desired output:

static void Main(string[] args)
{
   TestAll().Wait();
}

But look at synchronously waiting on asynchronous task problem. It is related to context threads (UI thread, ASP context thread,...) so it should be safe with Console app, but always good to know what is behind.

added 282 characters in body
Source Link
Tomas Chabada
  • 3k
  • 1
  • 20
  • 19

As you stated, the output indicates the synchronous processing, because it actually is a synchronous processing.

Defining Task does not mean asynchronous processing. There is no new Task creation, you only wrapped code to Task, but you did not create a new one.
If you want to test async processing, you should modify your method:

private Task<int> TestAsync()
{
    return Task.Run(() =>
    {
        for (int i = 0; i < 20000; i++)
            ;
        Console.WriteLine("Logic C");
        return Task.FromResult(0);
    });
}

To answer your another question:

You don't have desired output because your Main method is not waiting for asynchronous operation to finish. So as a execution in method TestAll goes to int value = await y; the main method will continue and will finish gracefully.

As you stated, the output indicates the synchronous processing, because it actually is a synchronous processing.

Defining Task does not mean asynchronous processing. There is no new Task creation, you only wrapped code to Task, but you did not create a new one.
If you want to test async processing, you should modify your method:

private Task<int> TestAsync()
{
    return Task.Run(() =>
    {
        for (int i = 0; i < 20000; i++)
            ;
        Console.WriteLine("Logic C");
        return Task.FromResult(0);
    });
}

As you stated, the output indicates the synchronous processing, because it actually is a synchronous processing.

Defining Task does not mean asynchronous processing. There is no new Task creation, you only wrapped code to Task, but you did not create a new one.
If you want to test async processing, you should modify your method:

private Task<int> TestAsync()
{
    return Task.Run(() =>
    {
        for (int i = 0; i < 20000; i++)
            ;
        Console.WriteLine("Logic C");
        return Task.FromResult(0);
    });
}

To answer your another question:

You don't have desired output because your Main method is not waiting for asynchronous operation to finish. So as a execution in method TestAll goes to int value = await y; the main method will continue and will finish gracefully.

deleted 41 characters in body
Source Link
Tomas Chabada
  • 3k
  • 1
  • 20
  • 19

As you stated, the output indicates the synchronous processing, because it actually is a synchronous processing.

Defining Task does not mean asynchronous processing. There is no new Task creation, you only wrapped code to Task, but you did not create a new one.
If you want to test async processing, you canshould modify your method signature and create a new Task:

private async Task<int> TestAsync()
{
    return await Task.Run(() =>
    {
        for (int i = 0; i < 20000; i++)
            ;
        Console.WriteLine("Logic C");
        return Task.FromResult(0);
    });
}

As you stated, the output indicates the synchronous processing, because it actually is a synchronous processing.

Defining Task does not mean asynchronous processing. There is no new Task creation, you only wrapped code to Task, but you did not create a new one.
If you want to test async processing, you can modify your method signature and create a new Task:

private async Task<int> TestAsync()
{
    return await Task.Run(() =>
    {
        for (int i = 0; i < 20000; i++)
            ;
        Console.WriteLine("Logic C");
        return Task.FromResult(0);
    });
}

As you stated, the output indicates the synchronous processing, because it actually is a synchronous processing.

Defining Task does not mean asynchronous processing. There is no new Task creation, you only wrapped code to Task, but you did not create a new one.
If you want to test async processing, you should modify your method:

private Task<int> TestAsync()
{
    return Task.Run(() =>
    {
        for (int i = 0; i < 20000; i++)
            ;
        Console.WriteLine("Logic C");
        return Task.FromResult(0);
    });
}
added 373 characters in body
Source Link
Tomas Chabada
  • 3k
  • 1
  • 20
  • 19
Loading
Source Link
Tomas Chabada
  • 3k
  • 1
  • 20
  • 19
Loading