Limit the Upload Speed

To limit the upload speed you have to use the BufferSize property of the RadUpload, the UploadStarted, ProgressChanged and UploadFinished events and a DispatcherTimer.

The concept behind limiting the speed is to pause the upload whenever the desired bandwidth is exceeded and to resume it after the difference has been compensated. For example, if you want the RadUpload to upload the files with a speed of 1024 b/sec, you have to check every time the progress has changed, whether the uploaded bytes are greater than the maximum bytes limitation (in this case speed * elapsed time). If yes, you have to pause the upload and meanwhile, as the time increases, the maximum bytes limitation will also increase. After it has become greater than the uploaded bytes you can resume the upload. To implement this concept you have to do the following:

Prepare the Fields and Properties

To set the initial speed limitation use the BufferSize property of the RadUpload. Also set the IsPauseEnabled to False in order to hide the UI that regards the pausing information. This is needed as you will pause and resume the upload programmatically and this will reflect the UI controls, too.

Example 1: Limiting the speed

<telerik:RadUpload x:Name="radUpload" 
                   BufferSize="1024" 
                   IsPauseEnabled="False" /> 
In order to implement the speed limitation you have to define some fields and properties, that will help you control the upload process.
  • timer - a field that will represent the DispatcherTimer.

  • startTime - a field that will store the start DateTime of the upload process.

  • UploadLimit - a property that returns the maximum bytes limit.

Example 2: Defining the example fields and properties

private DispatcherTimer timer; 
private DateTime startTime; 
public int UploadLimit 
{ 
    get 
    { 
        int elapsedSeconds = ( int )TimeSpan.FromTicks( DateTime.Now.Ticks - this.startTime.Ticks ).TotalSeconds; 
        return this.radUpload.BufferSize * elapsedSeconds; 
    } 
} 
Private timer As DispatcherTimer 
Private speedLimit As Integer 
Private startTime As DateTime 
Public ReadOnly Property UploadLimit() As Integer 
 Get 
  Dim elapsedSeconds As Integer = DirectCast(TimeSpan.FromTicks(DateTime.Now.Ticks - Me.startTime.Ticks).TotalSeconds, Integer) 
  Return Me.radUpload.BufferSize * elapsedSeconds 
 End Get 
End Property 

Set up the DispatcherTimer

The next step is to configure the DispatcherTimer so it suits your needs. Set its Interval property and attach an event handler to the Tick event.

Example 3: Set up the timer

public LimitUploadSpeed() 
{ 
    InitializeComponent(); 
    this.timer = new DispatcherTimer(); 
    this.timer.Interval = new TimeSpan( 0, 0, 0, 0, 500 ); 
    timer.Tick += this.timer_Tick; 
} 
private void timer_Tick( object sender, EventArgs e ) 
{ 
} 
Public Sub New() 
 InitializeComponent() 
 Me.timer = New DispatcherTimer() 
 Me.timer.Interval = New TimeSpan(0, 0, 0, 0, 500) 
 timer.Tick += Me.timer_Tick 
End Sub 
Private Sub timer_Tick(sender As Object, e As EventArgs) 
End Sub 

Handle the UploadStarted event

Next you have to attach an event handler to the UploadStarted event of the RadUpload.

Example 4: Attaching the UploadStarted event handler

<telerik:RadUpload x:Name="radUpload" 
                   UploadStarted="radUpload_UploadStarted" 
                   BufferSize="1024" 
                   IsPauseEnabled="True" /> 
In it you have to set the startTime field and start the timer.

Example 5: Setting the start time and start the timer

private void radUpload_UploadStarted( object sender, UploadStartedEventArgs e ) 
{ 
    this.startTime = DateTime.Now; 
    this.timer.Start(); 
} 
Private Sub radUpload_UploadStarted(sender As Object, e As UploadStartedEventArgs) 
 Me.startTime = DateTime.Now 
 Me.timer.Start() 
End Sub 

Handle the ProgressChanged event

Attach an event handler to the ProgressChanged event of the RadUpload.

Example 6: Attaching ProgressChanged the event handler

<telerik:RadUpload x:Name="radUpload" 
                   UploadStarted="radUpload_UploadStarted" 
                   ProgressChanged="radUpload_ProgressChanged" 
                   BufferSize="1024" 
                   IsPauseEnabled="True" /> 
This event notifies you whenever the progress of the upload process has changed. Use it to compare the uploaded bytes against the maximum bytes limitation and pause the upload process if needed.

Example 7: Implement the upload limitation logic

private void radUpload_ProgressChanged(object sender, RoutedEventArgs e) 
{ 
 if (this.radUpload.CurrentSession.UploadedBytes > this.UploadLimit) 
 { 
  this.radUpload.PauseUpload(); 
  if (!this.timer.IsEnabled) 
   this.timer.Start(); 
 } 
} 
Private Sub radUpload_ProgressChanged(sender As Object, e As RoutedEventArgs) 
 If Me.radUpload.CurrentSession.UploadedBytes > Me.UploadLimit Then 
  Me.radUpload.PauseUpload() 
  If Not Me.timer.IsEnabled Then 
   Me.timer.Start() 
  End If 
 End If 
End Sub 

Handle the timer's Tick event

In the handler for the times's Tick event you check whether the upload process should be resumed or not. If you resume it you also have to stop the timer.

Example 8: Implement the upload limitation logic

private void timer_Tick( object sender, EventArgs e ) 
{ 
    if ( this.radUpload.CurrentSession.UploadedBytes < this.UploadLimit ) 
    { 
        if ( this.timer.IsEnabled ) 
        { 
            this.radUpload.ResumeUpload(); 
            this.timer.Stop(); 
        } 
    } 
} 
Private Sub timer_Tick(sender As Object, e As EventArgs) 
 If Me.radUpload.CurrentSession.UploadedBytes < Me.UploadLimit Then 
  If Me.timer.IsEnabled Then 
   Me.radUpload.ResumeUpload() 
   Me.timer.Stop 
  End If 
 End If 
End Sub 

Handle the UploadFinished event

And last, you have to attach an event handler to the UploadFinished event of the RadUpload.

Example 9: Attaching the UploadFinished event handler

<telerik:RadUpload x:Name="radUpload" 
                   UploadStarted="radUpload_UploadStarted" 
                   ProgressChanged="radUpload_ProgressChanged" 
                   UploadFinished="radUpload_UploadFinished" 
                   BufferSize="1024" 
                   IsPauseEnabled="True" /> 
In it you have to stop the timer.

Example 10: Stoping the timer

private void radUpload_UploadFinished(object sender, RoutedEventArgs e) 
{ 
 this.timer.Stop(); 
} 
    Private Sub radUpload_UploadFinished(sender As Object, e As RoutedEventArgs) 
        Me.startTime = DateTime.Now 
        Me.timer.Stop 
    End Sub 

See Also

In this article