Simple File Transfer Part 2

<< Back to the .Networking page


This is a continuation of the previous tutorial. In this tutorial you will learn how to pause and resume files while transfering.

Download: SimpleFileTransfer2.zip
Source: SimpleFileTransfer2Src.zip
Unzip all files (executables plus dependancies) into a single folder

Introduction

Below is the full code required to make a minimal File Transfer program that allows users to pause and resume files. This example will be built based on the previous example.

Tutorial

1) Open the project of Simple File Transfer Part 1.

2) Add two buttons, a label, and a progress bar to the Form. After adding them, change the text and name of the buttons in Properties. Name the buttons to pauseButton and resumeButton. Change the text to Pause and Resume accordingly. Change the text of the new label to "Bytes: ". Rearrange the widgets as shown.

3) Add code.

  • Add code for pause. Double click the pause button. Add the following code in pauseButton_Click.
if(ft.Status == FileTransferStatus.Transferring)
{
  ft.Pause();
}
  • Add code for resume. Click the Design tap and double click the resume button. Add the following code in resumeButton_Click.
if(ft.Status == FileTransferStatus.Paused)
{
  ft.Resume();
}
  • Add progress event handler. Add the following code in subscription1_Notified. Again, use double tap after "+=".
ft.Progress += new EventHandler(ft_Progress);
  • Add code in the pregress event handler. Add the following code in ft_Progress.
ft = sender as FileTransfer;

// update progress bar
this.progressBar1.Maximum = ft.Length;
this.progressBar1.Value = ft.BytesTransferred;

// show bytes transfered
this.label2.Text = "Bytes: " + ft.BytesTransferred.ToString();
if (this.progressBar1.Value == ft.Length)
  this.label2.Text += "    Finished";

4) Compile.

5) Testing. Just like last time, run the Dictionary Monitor from the Start button. Click Open. In the popup window, the default Address should be "tcp://localhost:test". If not, type it in. Click Open. Now, here is something you should know before you move on. For optimization purpose, if a client download a file that is already exist in that client, the file will be copied instead of transferred. So in such case, the progress event will not be fired and thus nothing will happen in the progress bar. Also, for one or more clients, the first client that calls Open() in sharedDictionary1 will become the server of the Shared Dictionary. As a result, the first client will have all the files that are uploaded. Therefore, if the first client is used to upload or download files, the progress bar will not works. While keeping all these details in mind, you can open up three clients to test the program. Try only use the second and third clients for upload and download. You can pause and resume the process of file transferring.