private void RefreshAndRunTask(IUnitTestRun run, TaskCompletionSource <bool> tcs, Lifetime taskLifetime)
        {
            var cancellationTs    = run.GetData(ourCancellationTokenSourceKey);
            var cancellationToken = cancellationTs.NotNull().Token;

            myLogger.Trace("Before calling Refresh.");
            Refresh(run.Lifetime, tcs, cancellationToken).ContinueWith(__ =>
            {
                if (tcs.Task.IsCanceled || tcs.Task.IsFaulted) // Refresh failed or was stopped
                {
                    return;
                }

                myLogger.Trace("Refresh. OnCompleted.");
                // KS: Can't use run.Lifetime for ExecuteOrQueueEx here and in all similar places: run.Lifetime is terminated when
                // Unit Test Session is closed from UI without cancelling the run. This will leave task completion source in running state forever.
                mySolution.Locks.ExecuteOrQueueEx(myLifetime, "Check compilation", () =>
                {
                    if (!run.Lifetime.IsAlive || cancellationTs.IsCancellationRequested)
                    {
                        tcs.TrySetCanceled();
                        return;
                    }

                    if (myBackendUnityHost.BackendUnityModel.Value == null)
                    {
                        tcs.SetException(new Exception("Unity Editor connection unavailable."));
                        return;
                    }

                    var filters = GetFilters(run);

                    UnitTestLaunchClientControllerInfo unityClientControllerInfo = null;
                    var clientControllerInfo = run.HostController.GetClientControllerInfo(run);
                    if (clientControllerInfo != null)
                    {
                        unityClientControllerInfo = new UnitTestLaunchClientControllerInfo(
                            clientControllerInfo.AssemblyLocation,
                            clientControllerInfo.ExtraDependencies?.ToList(),
                            clientControllerInfo.TypeName);
                    }

                    var frontendBackendModel = mySolution.GetProtocolSolution().GetFrontendBackendModel();
                    myUsageStatistics.TrackActivity("UnityUnitTestPreference", frontendBackendModel.UnitTestPreference.Value.ToString());
                    var mode = TestMode.Edit;
                    if (frontendBackendModel.UnitTestPreference.Value == UnitTestLaunchPreference.PlayMode)
                    {
                        mode = TestMode.Play;
                    }
                    var launch = new UnitTestLaunch(run.Launch.Session.Id, filters, mode, unityClientControllerInfo);

                    myBackendUnityHost.BackendUnityModel.ViewNotNull(taskLifetime, (lt, model) =>
                    {
                        // recreate UnitTestLaunch in case of AppDomain.Reload, which is the case with PlayMode tests
                        myLogger.Trace("UnitTestLaunch.SetValue.");
                        if (frontendBackendModel.UnitTestPreference.Value == UnitTestLaunchPreference.Both)
                        {
                            model.UnitTestLaunch.SetValue(launch);
                            SubscribeResults(run, lt, launch);
                            launch.RunResult.Advise(lt, result =>
                            {
                                if (launch.TestMode == TestMode.Play)
                                {
                                    tcs.SetResult(result.Passed);
                                }
                                else
                                {
                                    launch = new UnitTestLaunch(launch.SessionId, launch.TestFilters, TestMode.Play,
                                                                launch.ClientControllerInfo);
                                    model.UnitTestLaunch.SetValue(launch);
                                    SubscribeResults(run, lt, launch);
                                    StartTests(run, tcs, lt);
                                }
                            });
                        }
                        else
                        {
                            model.UnitTestLaunch.SetValue(launch);
                            SubscribeResults(run, lt, launch);
                            launch.RunResult.Advise(lt, result => { tcs.SetResult(result.Passed); });
                        }
                    });

                    StartTests(run, tcs, taskLifetime);

                    // set results for explicit tests
                    foreach (var element in run.Elements.OfType <NUnitElementBase>().Where(a =>
                                                                                           a.RunState == RunState.Explicit && !run.Launch.Criterion.Explicit.Contains(a)))
                    {
                        myUnitTestResultManager.TestFinishing(element, run.Launch.Session,
                                                              "Test should be run explicitly", UnitTestStatus.Ignored);
                    }
                });
            }, cancellationToken);
        }