using percentProgressCallback with CopyFileToDevice

May 15, 2013 at 7:00 AM
Hi, firstly RAPI2 has been excellent for me and my needs. But I require an example on how to use the 5th parameter of the CopyFileToDevice function : Action<float> percentProgressCallback.
Specifically, I want to use it to update a progress bar while Im copying a largish (30mb+) file to the device. Ive tried using lamba expressions such as :
RemoteFile.CopyFileToDevice(Dev, PathFrom, PathTo, true, (i) => UpdateProgressBarMethod(i))
but Im not having any luck, any help would be greatly appreciated, thanks.
May 15, 2013 at 3:47 PM
It is a delegate method parameter looking for a method in the form of "void MethodName(float value)". The Action<T> delegate is defined by .NET.
public void OnCallback(float percent) { /* Do something */ }

RemoteFile.CopyFileToDevice(Dev, PathFrom, PathTo, true, OnCallback);
May 16, 2013 at 2:52 AM
Hi dahall,

Thanks for that. I took the OnCallBack method and added the following code :
public void OnCallback(float percent) 
{
            ProgessBar.Value = Convert.ToInt32(percent * 1000);
}
I gave the Progress Bar a Max value of 1000.
This sort of works, it doesn't do anything for awhile, then when the CopyFileToDevice function finishes, the bar progresses rapidly.
Ive tried adding ProgressBar.Update and Application.DoEvents to the method, but it doesn't make much difference.
Is this a threading issue ? what more would I need to do to have a live update of the Progress Bar? Keep in mind I am copying a 30mb+ cab file which takes a good minute or 2.

Thanks again.
May 16, 2013 at 6:14 PM
This method fires the callback method after every 4K of transfer. It does so using InvokeMethod so threading could be causing a problem.
May 17, 2013 at 2:05 AM
Thanks, so how should I go about it then? Do I need to use a Delegate?
May 17, 2013 at 3:15 PM
I would use separate threads to run the DeviceManager and another to have the progress reporting (UI thread/Device thread).
May 24, 2013 at 2:52 AM
Sorry mate, but is there any chance of a quick code example? Im still a bit of a newbie? :)
Thanks
May 24, 2013 at 4:15 PM
Use something like a BackgroundWorker and put all the RemoteDeviceManager, RemoteDevice and RemoteFile instantiations and method calls in its DoWork event handler. Create a method on your UI/form like the OnCallback method above. Pass that method pointer into the CopyFileToDevice method call. There is lots of information on using BackgroundWorker on the web. For this library, it cannot handle working across threads. You must create the RemoteDeviceManager and do all associated work with devices within a single thread. If you use BackgroundWorker, then you must do all interactions with this library in the DoWork event handler, like this:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   using (RemoteDeviceManager r = new RemoteDeviceManager())
   {
      using (RemoteDevice dev = r.Devices.FirstConnectedDevice)
      {
         if (dev == null)
            return;
         RemoteFile.CopyFileToDevice(dev, localFile, deviceFile, true, OnCallBack);
      }
   }
}
May 27, 2013 at 8:01 AM
Sweet, thanks for that, works like a charm :)
Jun 24, 2013 at 9:19 PM
Good evening

I would like to use the code with copyfiletodevcice with progressbar.
But I write in Vb net and I'am not familar with the delegate. I have try that :
Private Sub OnCallback(ByVal percent As Double)
    ProgressBar1.Value = Convert.ToInt32(percent * 1000)
End Sub
Public Delegate Sub DelOnCallBack(ByVal percent As Double)
Private Sub BGW_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim r As RemoteDeviceManager = New RemoteDeviceManager()
    Dim dev As RemoteDevice = mgr.Devices.FirstConnectedDevice
    If dev Is Nothing Then Return
    RemoteFile.CopyFileToDevice(dev, "D:\Temp\Robot_Phie_espace_23.csv", "\Temp\Robot_Phie_espace_23.csv", True, AddressOf (OnCallback()))
End Sub
But there is a error on AdressOf(OnCallBack).

Could you give me the code in VB for this example ?

Thank you