Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF C#: Using Tasks

Tags:

c#

wpf

task

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;
        }
like image 926
Martyn Ball Avatar asked Mar 12 '23 23:03

Martyn Ball


1 Answers

There's a built-in class Progress which would report the progress to UI thread.

public async void StartProcessingButton_Click(object sender, EventArgs e)
{
  // The Progress<T> constructor captures our UI context,
  //  so the lambda will be run on the UI thread.
  var progress = new Progress<int>(percent =>
  {
    textBox1.Text = percent + "%";
  });

  // DoProcessing is run on the thread pool.
  await Task.Run(() => DoProcessing(progress));
  textBox1.Text = "Done!";
}

public void DoProcessing(IProgress<int> progress)
{
  for (int i = 0; i != 100; ++i)
  {
    Thread.Sleep(100); // CPU-bound work
    if (progress != null)
      progress.Report(i);
  }
}

Read more about the Progress Reporter here with Async/Await.

Update -

Here's how you can do it in your code to report the progress.

Define the progress object with it's progresschanged handler. Below is a sample with anonymous handler.

  IProgress<UploadStep> progress = new Progress<UploadStep>(step =>
  {
      // sample consumption of progress changed event.
      if (step.Complete)
      {
          // Do what ever you want at the end.
      }
      else
      {
          statusTxtBox.Text = step.Message;
      }
  }); 

Invoke your DoUpload with this object of Progress

 DoUpload(driveInfo, progress);

Now following change in your DoUpload method:

public void DoUpload(DriveInfo sel, IProgress<UploadStep> progress)
{
    // Check that there is a device selected
    if (sel != null)
    {
        progress.Report(new UploadStep { Message = "First Task..." });

        // 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) });
            }

            progress.Report(new UploadStep { Message = "Generated Thumnails..." });

            foreach (string item in files)
            {
                // Generate new name
                Filenames.Add(
                    new NFile
                    {
                        OldName = Path.GetFileNameWithoutExtension(item),
                        NewName = Common.Security.KeyGenerator.GetUniqueKey(32)
                    });
            }

            progress.Report(new UploadStep { Message = "Uplaoding to database..." });
        }
    }
}
like image 193
vendettamit Avatar answered Mar 25 '23 10:03

vendettamit