-1
If ServerVersion > localVersion Then

                Net.ServicePointManager.DefaultConnectionLimit = 20

                Dim url As New Uri(sUrlToReadFileFrom)

                Dim request As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
                Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), System.Net.HttpWebResponse)
                response.Close()

                Dim iSize As Int64 = response.ContentLength

                Dim iRunningByteTotal As Int64 = 0

                Using client As New System.Net.WebClient()
                    Using streamRemote As System.IO.Stream = client.OpenRead(New Uri(sUrlToReadFileFrom))
                        Using streamLocal As Stream = New FileStream(sFilePathToWriteFileTo, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)
                            Dim iByteSize As Integer = 0
                            Dim byteBuffer(iSize - 1) As Byte
                            iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)
                            Do While iByteSize > 0


                                streamLocal.Write(byteBuffer, 0, iByteSize)
                                iRunningByteTotal += iByteSize


                                Dim dIndex As Double = CDbl(iRunningByteTotal)
                                Dim dTotal As Double = CDbl(byteBuffer.Length)
                                Dim dProgressPercentage As Double = (dIndex / dTotal)
                                Dim iProgressPercentage As Integer = CInt(Math.Truncate(dProgressPercentage * 100))

                                bgDownloader.ReportProgress(iProgressPercentage)
                                iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)
                            Loop

                            streamLocal.Close()
                        End Using

                        streamRemote.Close()
                    End Using

            End If

Using the above code in a BackgroundWorker on a VB.NET WPF Project. An error occurs when I try to start the code.

The calling thread cannot access this object because a different thread owns it.

This code works perfectly in WinForms project without any edits/modifications.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Then use invoke to change the object. Eg: Me.Invoke(Sub() Label1.Text="Something") or begininvoke if you don't need it immediately – CruleD Sep 19 '19 at 18:20
  • Possible duplicate of [The calling thread cannot access this object because a different thread owns it](https://stackoverflow.com/questions/9732709/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it) – ASh Sep 19 '19 at 19:04
  • @CruleD Where will I put this? Inside the DO Loop or in the DO_Work event of Background worker? – Hardcore Henry Sep 19 '19 at 19:22
  • @HardcoreHenry Wherever there is UI (main thread) stuff. – CruleD Sep 20 '19 at 08:21

1 Answers1

0

Thanks to @Cruled for providing the clue. It has been fixed. What I did is add invokes on all things needs updating.