Search code examples
vb.netmultithreadingwindows-cerapi

Using Thread Moving Files from PC to Remote Device


Using RAPI2 to send file from PC to a Remote Device.

When using the line without Threads, it works fine:

RemoteFile.CopyFileToDevice(PdtRemoteDevice, "C:\sample.txt", "\test\sample.txt", True)

But when I try to put that line above inside a Thread, it returns an error that the device is not connected.

How do I use RAPI2's CopyFileToDevice using Thread?

Not using Thread will make the application looks like it hanged. So, I don't think a user would appreciate an application that would says not responding.

My Code:

Imports System.Devices
Imports System.Devices.RemoteDeviceManager

Public Class FormHome
   Dim PdtRemoteDevice As RemoteDevice
   Dim Devicemanager As New RemoteDeviceManager

   Private Sub ButtonHomeCopyToDevice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonHomeCopyToDevice.Click
      PdtRemoteDevice = Devicemanager.Devices.FirstConnectedDevice

      If PdtRemoteDevice Is Nothing Then
         MessageBox.Show("No Device Connected, Please Try Again")
         Exit Sub
      Else
         Dim Th As Thread
         Th = New Thread(AddressOf UploadDatabase)
         Th.IsBackground = True
         Th.Start()
      End If
   End Sub

   Private Sub UploadDatabase()
      Using PdtRemoteDevice = Devicemanager.Devices.FirstConnectedDevice
         If Not (PdtRemoteDevice Is Nothing) Then
            RemoteFile.CopyFileToDevice(PdtRemoteDevice, "C:\sample.txt", "\test\sample.txt", True)
         End If
      End Using
   End Sub
End Class

Solution

  • Try this and let me know if it works:

    Private Sub UploadDatabase()
        Using rdm As New RemoteDeviceManager
            Using d As RemoteDevice = rdm.Devices.FirstConnectedDevice
                If Not (d Is Nothing) Then
                    RemoteFile.CopyFileToDevice( _
                        d, "C:\sample.txt", "\test\sample.txt", True)
                End If
            End Using
        End Using
    End Sub