2017년 4월 3일 월요일

[Development Story] GFtp v1.5 - Added progress form using BackgroundWorker that has a cancel button.

GFtp v1.5 has a Cancel button that can clicked while translating files.
This button is implemented using BackgroundWorker in C#.
First, I try to implement a cancel button function using modaless form.
But I failed. So I did googling and can find implementation about cancel button.
Link is following.
http://perschluter.com/show-progress-dialog-during-long-process-c-sharp/

Thanks to

Per Schlüter


1. Update list

 - Added a progress form that has a cancel button.

2. GFtp v1.5 demo

 - 

3. Code snippets

 - Required event handler.

  - backgroundWorker.DoWork
  - backgroundWorker.ProgressChanged
  - backgroundWorker.RunWorkerCompleted

 - backgroundWorker.DoWork

   The event handler is called by BackgroundWorker.RunWorkerAsync() call.
   This is running on thread. So you can work other things while running this event handler.
   I defined this event handler like following code.
this.backgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker_DoWork);
 
   I implemented this event handler like following code that to translate files.
 // The backgroundWorker for upload and download
        void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            // Get files of ftp
            string[] files;
            if(_ftpController.Upload == false)
                files   = ftpFileGridView.GetSelectedFiles();
            else
                files = fileGridView.GetSelectedFiles();

        
            // Translates files using ftpcontroler.
            // BackgroundWorker will changed percent while translating files.
            if (_ftpController.TranslateFiles(files, worker))
            {
                if (_ftpController.Upload == false)
                    RefreshFileGridViewOfCurrentDirectory();
                else
                    RefreshFtpFileGridView();

                if(worker.CancellationPending == true)
                    MessageBox.Show("Canceled.");
                else
                    MessageBox.Show("Completed.");
            }
            else
            {
                MessageBox.Show("Failed.");
            }
        }

 - backgroundWorker.ProgressChanged

   This event handler is called when progress percent changed.
   So I implemented this event handler for updateing progress bar.
        // When progress percent is changed, call this function
        void backgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            // title caption
            _progressForm.Text = "Translating " + _ftpController.CurrentTranslatingFile + "..." + " " + (e.ProgressPercentage.ToString() + "%");

            // label
            _progressForm.Message = _progressForm.Text;

            // progress bar percent.
            _progressForm.ProgressValue = e.ProgressPercentage;
        }


 - backgroundWorker.RunWorkerCompleted

   This event handler is called when backgroundWorker is done.
   I implemented this event handler for closing the progress form and refresh the file grid view.

        void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            _progressForm.Close();

            RefreshFileGridViewOfCurrentDirectory();
        }

 - Cancel button in the progress form.

   Because file translating is running on thread, you can click the cancel button in the progress form that showing.
   If you click the cancel button then call the event handler for cancel.
   The cancel event handler stop backgroundWorker and display message box "Canceled".

        // when cancel button is clicked, call this function
        void _progressForm_Canceled(object sender, EventArgs e)
        {
            if (backgroundWorker.WorkerSupportsCancellation == true)
            {
                // Cancel the asynchronous operation.
                backgroundWorker.CancelAsync();

                // Close the AlertForm
                _progressForm.Close();

                RefreshFileGridViewOfCurrentDirectory();
            }
        }

   Also, the cancel event handler is set for event handler of cancel button in the progress form like following.

       _progressForm = new ProgressForm();
       _progressForm.Canceled += _progressForm_Canceled;

4. Etc

 - Souce code : https://github.com/gurum77/GFtp
 - Plan : File translating using thread.

댓글 없음:

댓글 쓰기