Exemple #1
0
        public async Task DownloadFileTest()
        {
            await WebDavClientApi.DownloadFile("qb7cqjj2LcaQpNW", $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\1107798.cnf", _cts.Token);

            Assert.IsTrue(File.Exists($"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\1107798.cnf"));
            File.Delete($"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\1107798.cnf");
            Assert.IsFalse(File.Exists($"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\1107798.cnf"));
        }
        private async void MenuItemMenuSpectraAll_Click(object sender, EventArgs e)
        {
            if (_folderDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            _cts = new CancellationTokenSource();

            FooterStatusLabel.Text        = $"{_labels.GetLabel("InitAllSpectraDownloading")}";
            FooterStatusProgressBar.Value = 0;
            ButtonCancel.Enabled          = true;
            try
            {
                _cts = new CancellationTokenSource();
                var sliSpectras  = SpectraTools.SLISetSpectrasAsync(SetKey, _cts.Token).WithCancellation(_cts.Token);
                var lli1Spectras = SpectraTools.LLISetSpectrasAsync(SetKey, IrradiationType.LLI1, _cts.Token).WithCancellation(_cts.Token);
                var lli2Spectras = SpectraTools.LLISetSpectrasAsync(SetKey, IrradiationType.LLI2, _cts.Token).WithCancellation(_cts.Token);

                var taskSli = Task.Run(async() =>
                {
                    await foreach (var ssi in sliSpectras.WithCancellation(_cts.Token))
                    {
                        if (_cts == null)
                        {
                            _cts = new CancellationTokenSource();
                        }
                        await WebDavClientApi.DownloadFile(ssi.token, Path.Combine(_folderDialog.SelectedPath, SetKey, "SLI", ssi.SampleType, $"{ssi.SampleSpectra}.cnf"), _cts.Token);
                    }
                });

                var taskLLI1 = Task.Run(async() =>
                {
                    await foreach (var ssi in lli1Spectras.WithCancellation(_cts.Token))
                    {
                        if (_cts == null)
                        {
                            _cts = new CancellationTokenSource();
                        }
                        await WebDavClientApi.DownloadFile(ssi.token, Path.Combine(_folderDialog.SelectedPath, SetKey, SpectraTools.IrradiationTypeMap[IrradiationType.LLI1], ssi.LoadNumber.ToString(), $"c-{ssi.Container}", ssi.SampleType, $"{ssi.SampleSpectra}.cnf"), _cts.Token);
                    }
                });

                var taskLLI2 = Task.Run(async() =>
                {
                    await foreach (var ssi in lli2Spectras.WithCancellation(_cts.Token))
                    {
                        if (_cts == null)
                        {
                            _cts = new CancellationTokenSource();
                        }
                        await WebDavClientApi.DownloadFile(ssi.token, Path.Combine(_folderDialog.SelectedPath, SetKey, SpectraTools.IrradiationTypeMap[IrradiationType.LLI2], ssi.LoadNumber.ToString(), $"c-{ssi.Container}", ssi.SampleType, $"{ssi.SampleSpectra}.cnf"), _cts.Token);
                    }
                });

                await Task.WhenAll(taskSli, taskLLI1, taskLLI2);

                FooterStatusLabel.Text        = $"{_labels.GetLabel("AllSpectraDownldCompl")}";
                FooterStatusProgressBar.Value = FooterStatusProgressBar.Maximum;
            }
            catch (IndexOutOfRangeException)
            {
                MessageBox.Show(_labels.GetLabel("SpectraNotFound"), Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (OperationCanceledException ose)
            {
                FooterStatusProgressBar.Value = 0;
                FooterStatusLabel.Text        = _labels.GetLabel("CancelOperation");
            }
            catch (AggregateException ae)
            {
                foreach (var ie in ae.InnerExceptions)
                {
                    MessageBoxTemplates.WrapExceptionToMessageBox(new ExceptionEventsArgs()
                    {
                        exception = ie, Level = ExceptionLevel.Error
                    });
                }
            }
            finally
            {
                ButtonCancel.Enabled = false;
                _cts.Dispose();
                _cts = null;
            }
        }
        private async void MenuItemMenuSpectraLLI_Click(object sender, EventArgs e)
        {
            if (_folderDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            var objName = (sender as ToolStripMenuItem)?.Name;

            var type = objName.Contains("LLI1") ? IrradiationType.LLI1 : IrradiationType.LLI2;

            _cts = new CancellationTokenSource();
            ButtonCancel.Enabled = true;
            try
            {
                FooterStatusProgressBar.Value   = 0;
                FooterStatusProgressBar.Maximum = 1;
                var i = 0;

                await foreach (var ssi in SpectraTools.LLISetSpectrasAsync(SetKey, type, _cts.Token).WithCancellation(_cts.Token))
                {
                    if (_cts == null)
                    {
                        _cts = new CancellationTokenSource();
                    }
                    i++;
                    if (i % 2 == 0)
                    {
                        FooterStatusProgressBar.Value = 0;
                    }

                    FooterStatusLabel.Text = $"{_labels.GetLabel("DownloadingFile")}{ssi.SampleSpectra}";
                    await WebDavClientApi.DownloadFile(ssi.token, Path.Combine(_folderDialog.SelectedPath, SetKey, SpectraTools.IrradiationTypeMap[type], ssi.LoadNumber.ToString(), $"c-{ssi.Container}", ssi.SampleType, $"{ssi.SampleSpectra}.cnf"), _cts.Token);

                    FooterStatusProgressBar.Value = 1;
                }

                if (i == 0)
                {
                    throw new IndexOutOfRangeException();
                }

                FooterStatusLabel.Text = $"{_labels.GetLabel("DownloadingHasDone")}{i}";
            }
            catch (IndexOutOfRangeException)
            {
                MessageBox.Show(_labels.GetLabel("SpectraNotFound"), Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (OperationCanceledException ose)
            {
                try
                {
                    FooterStatusProgressBar.Value = 0;
                    FooterStatusLabel.Text        = _labels.GetLabel("CancelOperation");
                }
                catch (NullReferenceException)
                { }
            }
            catch (AggregateException ae)
            {
                foreach (var ie in ae.InnerExceptions)
                {
                    MessageBoxTemplates.WrapExceptionToMessageBox(new ExceptionEventsArgs()
                    {
                        exception = ie, Level = ExceptionLevel.Error
                    });
                }
            }
            finally
            {
                ButtonCancel.Enabled = false;
                _cts?.Dispose();
                _cts = null;
            }
        }