/*
        private void onLoadRegions()
        {
            if (!ensureFileNameSpecified()) return;
            ensureRegionsLoaded();

            var loader = new RuelectCsvLoader(FileName);
            string[] regions = null;
            try
            {
                regions = loader.LoadRegions();
                log("Extracted " + regions.Length + " regions");
            }
            catch (InvalidDataException ex)
            {
                log(ex.ToString());
                //MessageBox.Show(ex.ToString());
                return;
            }
            finally
            {
                loader.Dispose();
            }

            var createCount = m_repository.UpdateRegions(regions);
            log("Created " + createCount + " regions");
        }
        */
        private Task checkRegions()
        {
            Tcs = new CancellationTokenSource();

            TaskLoading = Task.Factory.StartNew(
                () =>
                    {
                        if (!ensureFileNameSpecified()) return;
                        Repository.Initialize();
                        if (DownloadCsv)
                        {
                            FileName = downloadAndSaveCsv();
                            if (String.IsNullOrEmpty(FileName))
                                return;
                        }
                        var loader = new RuelectCsvLoader(FileName);
                        try
                        {
                            loader.CheckRegions(Repository);
                            log("All regions from csv-file exist in DB");
                        }
                        catch (InvalidDataException ex)
                        {
                            log(ex.ToString());
                            //MessageBox.Show(ex.ToString());
                        }
                        finally
                        {
                            loader.Dispose();
                        }
                    }, TaskCreationOptions.LongRunning)
                .ContinueWith(tearDown);

            return TaskLoading;
        }
        internal Task loadProtocols()
        {
            Tcs = new CancellationTokenSource();

            TaskLoading = Task.Factory.StartNew(
                () =>
                    {
                        if (!ensureFileNameSpecified()) return;
                        IsLoading = true;
                        Repository.Initialize();
                        if (DownloadCsv)
                        {
                            FileName = downloadAndSaveCsv();
                            if (String.IsNullOrEmpty(FileName ))
                                return;
                        }
                        bool isNewProvider;
                        string providerName = String.IsNullOrWhiteSpace(ProviderName) ? DefaultProviderName : ProviderName;
                        ResultProvider provider = Repository.GetOrCreateProvider(providerName , out isNewProvider);

                        var poll = ensurePollCreated();
                        provider.Poll = poll;

                        if (Tcs.IsCancellationRequested)
                            return;

                        var loader = new RuelectCsvLoader(FileName);
                        loadProtocols(loader, provider, isNewProvider);
                    }, TaskCreationOptions.LongRunning)
                .ContinueWith(tearDown);

            return TaskLoading;
        }