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.
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.
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.
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.
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.
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
?
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With