I have read afew different posts and I really can't get my head around this!
I have got the following class, when the user has selected a drive and set DriveInfo sel
the DoUpload
method runs, and converts a load of images to byte format.
Now this takes seconds on my machine, however with more images and slower machines it could take longer, so I want to do this on another thread, but each time an image is converted, I want to notify the UI, in my case I want to update the following Properties: Thumbnails, Filenames, Steps, UploadProgress.
Again, I want to update the above Properties each time an image is converted, how do I do this using the Task API?
Here is the class:
public class UploadInitiation : Common.NotifyUIBase
{
#region Public Properties
/// <summary>
/// File lists
/// </summary>
/// public ObservableCollection<BitmapImage> Thumbnail_Bitmaps { get; set; } = new ObservableCollection<BitmapImage>();
public ObservableCollection<ByteImage> Thumbnails { get; set; } = new ObservableCollection<ByteImage>();
public ObservableCollection<NFile> Filenames { get; set; } = new ObservableCollection<NFile>();
/// <summary>
/// Updates
/// </summary>
public ObservableCollection<UploadStep> Steps { get; set; } = new ObservableCollection<UploadStep>();
public int UploadProgress { get; set; } = 45;
public string UploadTask { get; set; } = "Idle...";
public bool UploadEnabled { get; set; } = false;
private bool _uploadBegin;
public bool UploadBegin
{
set { _uploadBegin = value; RaisePropertyChanged(); }
get { return _uploadBegin; }
}
#endregion END Public Properties
public UploadInitiation()
{
// Populate steps required, ensure upload returns UI updates
Steps.Add(new UploadStep { Message = "First task...", Complete = true, Error = null });
Steps.Add(new UploadStep { Message = "Error testing task...", Complete = false, Error = "testing error" });
Steps.Add(new UploadStep { Message = "Seperate upload to new thread...", Complete = false, Error = null });
Steps.Add(new UploadStep { Message = "Generate new file names...", Complete = false, Error = null });
Steps.Add(new UploadStep { Message = "Render Thumbnails, add to database...", Complete = false, Error = null });
Steps.Add(new UploadStep { Message = "Move images ready for print...", Complete = false, Error = null });
}
/// <summary>
/// This Method will perform the upload on a seperate thread.
/// Report progress back by updating public properties of this class.
/// </summary>
/// <param name="sel"></param>
public void DoUpload(DriveInfo sel)
{
// Check that there is a device selected
if (sel != null)
{
// Generate List of images to upload
var files = Directory.EnumerateFiles(sel.Name, "*.*", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".jpeg") || s.EndsWith(".jpg") || s.EndsWith(".png"));
if (files.Count() > 0)
{
// Manage each image
foreach (string item in files)
{
// Generate thumbnail byte array
Thumbnails.Add(new ByteImage { Image = GenerateThumbnailBinary(item) });
}
foreach (string item in files)
{
// Generate new name
Filenames.Add(
new NFile
{
OldName = Path.GetFileNameWithoutExtension(item),
NewName = Common.Security.KeyGenerator.GetUniqueKey(32)
});
}
}
}
}
public byte[] GenerateThumbnailBinary(string loc)
{
BitmapImage image = new BitmapImage(new Uri(loc));
Stream stream = File.OpenRead(loc);
byte[] binaryImage = new byte[stream.Length];
stream.Read(binaryImage,0,(int)stream.Length);
return binaryImage;
}