Пример #1
0
        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            if (running)
            {
                tokenSource.Cancel();
                btnStart.IsEnabled = false;
                return;
            }
            inputFile = new FileInfo(txtSrc.Text.Trim());
            if (!inputFile.Exists)
            {
                MessageBox.Show($"{txtSrc.Text}がないよう");
                return;
            }
            Prepare();
            progressBar.Maximum = inputFile.Length / blockZise;
            progressBar.Minimum = 0;
            btnStart.Content    = "Stop";
            btnStart.IsEnabled  = false;

            running = true;
            if (tokenSource == null)
            {
                tokenSource = new CancellationTokenSource();
            }
            var token = tokenSource.Token;

            Task.Factory.StartNew(() =>
            {
                Dispatcher.Invoke(() => btnStart.IsEnabled = true);
                long n = 0;
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    if (taskCount >= MaxTask)
                    {
                        Thread.Sleep(100);
                        continue;
                    }
                    n++;
                    var f = System.IO.Path.Combine(rootDir.FullName, $"{n:D16}{inputFile.Extension}");
                    Dispatcher.Invoke(() => {
                        progressBar.Value = 0;
                        lblState.Content  = $"{f} Free:{Nira.GetDriveFreeSpace(rootDir.FullName)}";
                    });
                    if (File.Exists(f))
                    {
                        continue;
                    }
                    WriteFile(f, token);
                    Thread.Sleep(1);
                }
            }, token).ContinueWith(t =>
            {
                tokenSource.Dispose();
                tokenSource = null;
                running     = false;

                foreach (var n in Tasks.ToArray())
                {
                    var boo = n.Result;
                }
                if (tmpDir.Exists)
                {
                    tmpDir.Delete();
                }

                Dispatcher.Invoke(() =>
                {
                    lblState.Content   = "done";
                    btnStart.IsEnabled = true;
                    btnStart.Content   = "Start";
                });
            });
        }
Пример #2
0
        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            if (running)
            {
                tokenSource.Cancel();
                btnStart.IsEnabled = false;
                return;
            }
            btnStart.Content   = "Stop";
            btnStart.IsEnabled = false;
            Prepare();
            running = true;
            if (tokenSource == null)
            {
                tokenSource = new CancellationTokenSource();
            }
            var token = tokenSource.Token;

            spool.Omit();
            Task.Factory.StartNew(() =>
            {
                Dispatcher.Invoke(() => btnStart.IsEnabled = true);
                long n = 0;
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }
                    if (MaxTask <= 0)
                    {
                        n++;
                        var f = System.IO.Path.Combine(rootDir.FullName, $"{n:D16}.{fileExt}");
                        Dispatcher.Invoke(() => lblProgress.Content = $"{f} Free:{Nira.GetDriveFreeSpace(rootDir.FullName)}");
                        if (File.Exists(f))
                        {
                            continue;
                        }
                        WriteFile(f);
                    }
                    else
                    {
                        if (spool.TaskCount >= MaxTask)
                        {
                            Thread.Sleep(100);
                            continue;
                        }
                        var dfs = Nira.GetDriveFreeSpace(rootDir.FullName);
                        if (dfs < 10)
                        {
                            break;
                        }

                        n++;
                        var f = System.IO.Path.Combine(rootDir.FullName, $"{n:D16}.{fileExt}");
                        Dispatcher.Invoke(() => lblProgress.Content = $"{f} Free:{dfs}");
                        if (File.Exists(f))
                        {
                            continue;
                        }
                        WriteFileAsync(f);
                    }
                    Thread.Sleep(1);
                }
            }, token).ContinueWith(t =>
            {
                tokenSource.Dispose();
                tokenSource = null;
                running     = false;
                spool.Stop();
                spool.Wait();

                if (tmpDir.Exists)
                {
                    tmpDir.Delete();
                }

                Dispatcher.Invoke(() =>
                {
                    btnStart.IsEnabled = true;
                    btnStart.Content   = "Start";
                });
            });
        }
Пример #3
0
        private bool WriteFile(string path, CancellationToken token)
        {
            if (File.Exists(path))
            {
                return(true);
            }

            var free = Nira.GetDriveFreeSpace(rootDir.FullName);

            if (free < readBuffer.Length)
            {
                return(false);
            }
            bool cancel = false;

            var  tmp = tmpDir.FullName + $"\\{Guid.NewGuid().ToString()}...tmp";
            var  fs  = new FileStream(tmp, FileMode.OpenOrCreate);
            long bc  = 0;

            using (var src = new FileStream(inputFile.FullName, FileMode.Open, FileAccess.Read)) {
                while (true)
                {
                    Dispatcher.Invoke(() => progressBar.Value = ++bc);
                    var s = src.Read(readBuffer, 0, readBuffer.Length);
                    if (s <= 0)
                    {
                        break;
                    }
                    if (token.IsCancellationRequested)
                    {
                        cancel = true;
                        break;
                    }

                    fs.Write(readBuffer, 0, s);
                }
            }
            Task <bool> task = null;

            task = new Task <bool>(() =>
            {
                Tasks.Add(task);
                taskCount++;
                fs.Flush();
                fs.Close();
                fs.Dispose();
                fs = null;
                if (cancel)
                {
                    File.Delete(tmp);
                }
                else
                {
                    File.Move(tmp, path);
                    FixTimestamp(path);
                }

                return(true);
            });
            task.ContinueWith(x =>
            {
                taskCount--;
                Tasks.Remove(task);
                return(true);
            });
            task.Start();

            return(true);
        }