I read several different posts and I really can't think it over!
I have the following class when a user selects a drive and sets a DriveInfo selmethod DoUpload, and converts the loading of images into byte format.
Now it takes a few seconds on my machine, however with a lot of images and slow machines this may take longer, so I want to do it in a different thread, but every time the image is converted, I want to notify the user interface in my I want to update following properties: thumbnails, file names, steps, UploadProgress.
Again, I want to update the above properties every time the image is converted, how can I do this using the task APIs?
Here is the class:
public class UploadInitiation : Common.NotifyUIBase
{
#region Public Properties
public ObservableCollection<ByteImage> Thumbnails { get; set; } = new ObservableCollection<ByteImage>();
public ObservableCollection<NFile> Filenames { get; set; } = new ObservableCollection<NFile>();
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()
{
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 });
}
public void DoUpload(DriveInfo sel)
{
if (sel != null)
{
var files = Directory.EnumerateFiles(sel.Name, "*.*", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".jpeg") || s.EndsWith(".jpg") || s.EndsWith(".png"));
if (files.Count() > 0)
{
foreach (string item in files)
{
Thumbnails.Add(new ByteImage { Image = GenerateThumbnailBinary(item) });
}
foreach (string item in files)
{
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;
}