VB.NET 项目线程问题

VB.NET project threading issue

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

在 VB.NET WPF 项目的 BackgroundWorker 中使用上述代码。尝试启动代码时出现错误。

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

此代码在没有任何 edits/modifications.

的 WinForms 项目中完美运行

感谢@Cruled提供线索。它已被修复。 我所做的是对所有需要更新的东西添加调用。