Question

I move files with

foreach(file){
file.move(file,dst);}

I want to update a form after each file was moved (different partitions) with information about progress. Unfortunately my WPF form is busy during entire copy/move queue. I tried to InvalidateVisual(), without success- what can I do to ensure GUI responsiveness during (or after) moving a file?

Was it helpful?

Solution 2

If you would like to keep your GUI responsive you should start a new thread doing this long task.

This way your GUI thread is not going to be blocked by this long operation.

class example
{
    static void UpdateStatus(int i)
    {
        // UpdateStatus
        Console.WriteLine(i + "Done!");
    }

    static void Main(string[] args)
    {
        int state = 0;
        Task t = Task.Run(
              () =>
              {
                  // do your stuff - this is going to run async
                  for (int i = 0; i < 10; i++)
                      file.move(file,dst);
                      UpdateStatus(i); // callback to update status 
                  }
              }
            );
        Console.WriteLine("Started"); // You wont block this code by running the async part
        t.Wait(); 
        Console.WriteLine("DONE");
        Console.ReadKey();

    }
}

In your case you could use like this:

        Task t = Task.Run(
              () =>
              {
                  // do your stuff - this is going to run async
                  foreach(File file in Files){
                  {
                      move(file);
                      UpdateStatus(i); // callback to update status (eg.:how many files have been moved yet)
                  }
              }
            );

OTHER TIPS

Kasan, I think the BackgroundWorker will be useful for you. Task is nice way but to update UI progress from task you will need to bother with dispatching events to the UI thread, otherwise you will get exception, because it's not allowed to update UI from threads other than UI. Here is a sample and link to the documentation

 public partial class MainWindow : Window
{
    BackgroundWorker _worker = new BackgroundWorker();
    public MainWindow()
    {
        InitializeComponent();
        _worker = new BackgroundWorker();
        _worker.DoWork += worker_DoWork;
        _worker.ProgressChanged += worker_ProgressChanged;
    }

    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progress1.Value = e.ProgressPercentage;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        _worker.RunWorkerAsync();
    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        var files = new List<string>();
        foreach(var file in files)
        {
            File.Move(file, /*target*/);
            _worker.ReportProgress(/* here is progress from 0 to 100 */)
        }
    }
}

BackgroundWorker Sample

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top