Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this C# 4.0 async method get called?

I'm trying to write a really simple bit of async code. I have a void method that doesn't take any parameters, which is to be called from a Windows service. I want to kick it off async, so that the service doesn't have to hang around waiting for the method to finish.

I created a very simple test app to make sure I was doing the coding right, but the async method just isn't being called. Anyone able to see what I've done wrong? I'm using .NET 4.0 by the way, so I can't use await (which would be a whole lot simpler!).

Here is my entire test sample...

using System;
using System.Threading;

namespace AsyncCallback {
  internal class Program {
    private static void Main(string[] args) {
      Console.WriteLine(DateTime.Now.ToLocalTime().ToLongTimeString() + " - About to ask for stuff to be done");
      new Action(DoStuff).BeginInvoke(ar => StuffDone(), null);
      Console.WriteLine(DateTime.Now.ToLocalTime().ToLongTimeString() + " - Asked for stuff to be done");
    }

    private static void StuffDone() {
      Console.WriteLine(DateTime.Now.ToLocalTime().ToLongTimeString() + " - Stuff done");
    }

    private static void DoStuff() {
      Console.WriteLine(DateTime.Now.ToLocalTime().ToLongTimeString() + " - Starting to do stuff");
      Thread.Sleep(1000);
      Console.WriteLine(DateTime.Now.ToLocalTime().ToLongTimeString() + " - Ending doing stuff");
    }
  }
}

Thanks for any help you can give.

like image 896
Avrohom Yisroel Avatar asked Dec 10 '12 19:12

Avrohom Yisroel


People also ask

Why there is no string in C?

There is no string type in C . You have to use char arrays. By the way your code will not work ,because the size of the array should allow for the whole array to fit in plus one additional zero terminating character.

What does C++ have that C does not?

C++ was developed by Bjarne Stroustrup in 1979. C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming. C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language.

Why is C not A or B?

Because C comes after B The reason why the language was named “C” by its creator was that it came after B language. Back then, Bell Labs already had a programming language called “B” at their disposal.

Why is C difficult?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


3 Answers

Your program is terminating before the async method gets deployed in the ThreadPool. Keep your program open for a bit. Perhaps Console.ReadKey() at the end of Main?

like image 116
spender Avatar answered Sep 25 '22 17:09

spender


Any application will end as soon as there are no foreground threads that haven't completed execution. If there are any background threads they will not keep the process "alive".

Because you're starting the async task in the background, and nothing is keeping the foreground thread running, the entire process is exiting.

You either need to run your asynchronous task in a foreground thread instead (which can't be done with BeginInvoke as far as I know, you'd need to create a new Thread explicitly) or do something else to block the main thread until it finishes.

like image 30
Servy Avatar answered Sep 22 '22 17:09

Servy


You are not waiting for completion, so try something like this

 Console.WriteLine(DateTime.Now.ToLocalTime().ToLongTimeString() + " - About to ask for stuff  to be done");
 var a = new Action(DoStuff);
 var iar = a.BeginInvoke(ar => StuffDone(), null);
 a.EndInvoke(iar);
 Console.WriteLine(DateTime.Now.ToLocalTime().ToLongTimeString() + " - Asked for stuff to be done");
like image 42
Kleinux Avatar answered Sep 25 '22 17:09

Kleinux