代码中断 - 这是因为线程

时间:2017-10-15 15:55:37

标签: wpf vb.net multithreading visual-studio

这是代码,

  • 我要么等待执行GetData() - 这会给出错误,说“调用线程无法访问此对象,因为另一个线程拥有它”

    :msg, contains, "http access_log"      @1192.168.1.10:514 # UDP Syslog
    

尝试将其设为同步功能 - 仍然无法正常工作

  • 或延迟200ms。

        BorrowedList.ItemsSource = new List(Of book) (await BookService.GetBooksBorrowed(UID))
    

这是电话

 Public Async Sub GetData(uid As String)
dim data as ArrayList = await MemberService.GetMember(UID)
BorrowedList.ItemsSource = new List(Of book) (await BookService.GetBooksBorrowed(UID))
LblUID.Content = UID
LblName.Content = data(0) + "" + data(1)
LblPhone.Content = data(2)
LblDepartment.Content = data(3)
LblSemester.Content = data(4)
_dashBoard.MemberPopup.Content = Me
_dashBoard.MemberPopupDialog.IsOpen = True
End Sub

1 个答案:

答案 0 :(得分:0)

当我试图用其他理论来设置therad时,错误被激怒了;这是不可能的,因为WPF在STA [Singe Thread Apartment]上运行。 要解决这个问题,我必须通过调用Dispatcher.invoke从其他线程调用UI线程。

更正的代码是:
   Public Async Sub GetData(uid As String)
    dim data as ArrayList = await MemberService.GetMember(UID)
    Dim x = new List(Of book) (await BookService.GetBooksBorrowed(UID))
    Me.Dispatcher.invoke(Sub()
             BorrowedList.ItemSource = x
             LblUID.Content = UID
             LblName.Content = data(0) + "" + data(1)
             LblPhone.Content = data(2)
             LblDepartment.Content = data(3)
             LblSemester.Content = data(4)
             _dashBoard.MemberPopup.Content = Me
             _dashBoard.MemberPopupDialog.IsOpen = True
         End Sub)  
End Sub