Using Task.Wait() for waiting while task is finished (even if it already finished) 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;
    }

}
You can use a CancellationToken and Task.WaitAll(...) to do this...

public class A
{
    private readonly CancellationTokenSource tokenSource = new CancellationTokenSource();
    private Task task1;
    private Task task2;

    public void Connect()
    {
        task1 = Task.Factory.StartNew(SomeWork, tokenSource.Token);
        task2 = Task.Factory.StartNew(SomeWork, tokenSource.Token);
    }

    public void Disconnect()
    {
        tokenSource.Cancel();
        Task.WaitAll(task1,task2);
    }

    public void SomeWork(object o)
    {
        CancellationToken token = (CancellationToken)o;
        while (!token.IsCancellationRequested)
        {
            Console.WriteLine("working...");
        }
    }
}


For more information on managed threads check this and this article on MSDN. Enjoy :D