Using C# in WPF, together with LINQ to SQL and LINQ for quering my generic collections, I noticed that sometimes, when I use several methods one after another and results of the first one are needed for next function, I should care about finishing actions of first function before starting next.
SomeMethod()
{
FirstMethod();
NextMethod();
....
}
Mostly it is related to methods, quering database, when updated results are used by next method.
Is it possible to sum up when the possibility of “not yet updated for continuing” can take place in c# and how this issue is generally solved?
Unless you’re doing multi-threading, it usually is not too hard to avoid the situation where you continue before you’re ready. In most situations where you call a method or ask for data, your next instruction is not executed until the previous instruction is done or at least ready.
(For example, you can start using a DataReader it will return control to your code before it has all the data, but it still protects you from “getting ahead of yourself”. it won’t give you a false “end”. If you use it faster than it gets the data, it will force your next instruction to wait rather than mess up your logic. And if you’re defined Linq expression hasn’t actually executed yet, it always will, in time, when you ask for its results.)
.Net protects you pretty well from asynchronous problems, unless you ask for them. If you’re multi-threading, or triggering an external process (web service or something) somewhere else and then assuming instantly that it’s finished and you can depend on its outcome, then you’ll have problems. But in general, just code away.
It has sometimes been tricky for me dealing with asynchronous issues, but they have never really taken me by surprise.