Can I use task.Wait();
like that? Note that when I call task.Wait
the task is probably already finished.
And probably you can suggest better pattern.
class A {
private Task task1;
private Task task2;
...
public void Connect() {
stayConnected = true;
task1 = Task.Factory.StartNew(....,
while (stayConnected) {
....
}
LongRunning);
task2 = Task.Factory.StartNew(....,
while (stayConnected) {
....
}
LongRunning);
}
private volatile bool stayConnected;
// should be synchronous. when return everything should be disconected
public void Disconnect() {
stayConnected = false;
task1.Wait();
task1 = null;
task2.Wait();
task2 = null;
}
}