Skip to content

Commit

Permalink
+ Fixed a bug in cancellinig tasks when a group of items were added a…
Browse files Browse the repository at this point in the history
…t once

+ #v0.0.0.2 is ready
  • Loading branch information
mhmd-azeez committed Nov 12, 2016
1 parent d0696c2 commit 6d3d151
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 19 deletions.
4 changes: 2 additions & 2 deletions RudeFox.FrontEnd/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.0.1")]
[assembly: AssemblyFileVersion("0.0.0.1")]
[assembly: AssemblyVersion("0.0.0.2")]
[assembly: AssemblyFileVersion("0.0.0.2")]
20 changes: 20 additions & 0 deletions RudeFox.FrontEnd/Services/ShredderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ await Task.Run(() =>

return true;
}

public bool IsFileLocked(FileInfo file)
{
FileStream stream = null;

try
{
stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
return true;
}
finally
{
if (stream != null) stream.Close();
}

return false;
}
#endregion
}
}
58 changes: 41 additions & 17 deletions RudeFox.FrontEnd/ViewModels/MainWindowVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,44 @@ void IDropTarget.Drop(IDropInfo dropInfo)
if (data.GetDataPresent(DataFormats.FileDrop))
{
string[] paths = (string[])data.GetData(DataFormats.FileDrop);
DeleteItems(paths);
DeleteItems(paths.ToList());
}
}
#endregion

#region Methods
private async void DeleteItems(string[] paths)
private async void DeleteItems(List<string> paths)
{
string message;
if (paths.Length == 1)
if (paths.Count == 1)
message = "Are you sure you want to delete this item?";
else
message = $"Are you sure you want to delete {paths.Length} items?";
message = $"Are you sure you want to delete {paths.Count} items?";

var response = MessageBox.Show(message, "Deleting items", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
if (response != MessageBoxResult.Yes) return;

var newItems = new List<WorkItemVM>();
var cts = new CancellationTokenSource();

var duplicates = from i in WorkItems
join p in paths
on i.Path equals p
select p;
paths.RemoveAll(p => duplicates.Contains(p));

foreach (var path in paths)
{
var item = new WorkItemVM();
item.Path = path;

if (File.Exists(path))
{
if (ShredderService.Instance.IsFileLocked(new FileInfo(path)))
{
MessageBox.Show($"Can't access file, it's being used by another application: {path}");
continue;
}
item.Type = ItemType.File;
newItems.Add(item);
}
Expand All @@ -105,29 +116,42 @@ private async void DeleteItems(string[] paths)

item.DeleteRequested += (sender, canceled) =>
{
if (canceled) cts.Cancel();
WorkItems.Remove(sender as WorkItemVM);
sender = null;
};

item.CancellationTokenSource = new CancellationTokenSource();
WorkItems.Add(item);
}

foreach (var item in newItems)
var tasks = newItems.Select(item =>
{
try
{
var progress = new Progress<double>();
progress.ProgressChanged += (sender, percent) =>
{
item.Progress = percent * 100;
};
await ShredderService.Instance.ShredItemAsync(item.Path, cts.Token, progress);
}
catch (OperationCanceledException)
{
return ShredderService.Instance.ShredItemAsync(item.Path, item.CancellationTokenSource.Token, item.TaskProgress);
});

try
{
await Task.WhenAll(tasks);
}
catch (OperationCanceledException)
{

}
catch (AggregateException exc)
{
var msg = $"{exc.InnerExceptions.Count()} error(s) occured: ";
foreach(var ex in exc.InnerExceptions)
{
msg += "\n---------------------------";
msg += Environment.NewLine;
msg += ex.ToString();
}
MessageBox.Show(msg);
}
catch( Exception exc)
{
var failed = tasks.Where(t => t.IsFaulted);
MessageBox.Show(exc.ToString());
}
}
#endregion
Expand Down
9 changes: 9 additions & 0 deletions RudeFox.FrontEnd/ViewModels/WorkItemVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

Expand All @@ -21,6 +22,9 @@ public WorkItemVM()
{
OnDeleteRequested(true);
});
CancellationTokenSource = new CancellationTokenSource();
TaskProgress = new Progress<double>();
TaskProgress.ProgressChanged += (sender, percent) => Progress = percent * 100;
}
#endregion

Expand Down Expand Up @@ -111,6 +115,10 @@ public string Size
}
}
}

public CancellationTokenSource CancellationTokenSource { get; set; }
public Progress<double> TaskProgress { get; set; }

#endregion

#region Events
Expand All @@ -133,6 +141,7 @@ private void OnDeleteRequested(bool canceled = false)
{
var handler = DeleteRequested;
handler?.Invoke(this, canceled);
if (canceled) CancellationTokenSource.Cancel();
}
#endregion
}
Expand Down

0 comments on commit 6d3d151

Please sign in to comment.