【问题标题】:Silverlight WCF - Invalid cross thread Exception with busy indicatorSilverlight WCF - 带有忙碌指示器的无效跨线程异常
【发布时间】:2014-11-01 09:53:09
【问题描述】:

我正在 Silverlight 中实现 WCF。

在 Silverlight 中调用 WCF 服务时,我想向用户显示“BusyIndi​​cator”。

我在link 遇到了实施busyindicator。

这是在 C# 中。我正在 vb.net 中开发项目。

无法正确地将其转换为 vb.net

busyIndicator.IsBusy = true;
//busyIndicator.BusyContent = "Fetching Data...";

ThreadPool.QueueUserWorkItem((state) =>
{
Thread.Sleep(3 * 1000);
Dispatcher.BeginInvoke(() => busyIndicator.IsBusy = false);
}

在我的要求中实现上述代码时

VB.Net中的代码如下:

busyIndicator.IsBusy = True
ThreadPool.QueueUserWorkItem(Sub(state)

      Dim s As New services.ServiceClient

      AddHandler s.confirmticketCompleted, AddressOf mcompleted
          s.confirmticketAsync()
           Dispatcher.BeginInvoke(Sub()
       busyIndicator.IsBusy = False     
                    End Sub)
                    End Sub)



Sub mcompleted(sender As System.Object, e As services.confirmticketCompletedEventArgs)

        MessageBox.Show(e.Result)

    End Sub

其中 services.serviceclient 是带有变量 s 的 WCF 代理

需要使用已完成的事件处理程序来获取返回值。

实现代码后,我得到“无效的跨线程访问”。例外在 MessageBox.Show(e.Result)

我的问题类似于link available at StackOverflow

但我听不懂 Ken2k 先生在说什么。

请指导并帮助我解决问题。

【问题讨论】:

    标签: vb.net wcf silverlight


    【解决方案1】:

    使用调度程序 Dispatcher.BeginInvoke(MessageBox.Show("BLAH") 包装 MessageBox.Show() 服务必须在不同于 UI 线程的线程上返回,这就是您收到异常的原因。

    【讨论】:

      【解决方案2】:

      知道了...

      我需要在完成的函数中提供busyindicator.isbusy=false。

      完全删除 ThreadPool.QueueUserWorkItem 和 Dispatcher.BeginInvoke。

      最终代码如下所示:

          busyIndicator.IsBusy = True
      
              Dim s As New services.ServiceClient
              AddHandler s.confirmticketCompleted, AddressOf mcompleted
              s.confirmticketAsync()
      
      
      
      Sub mcompleted(sender As System.Object, e As services.confirmticketCompletedEventArgs)
      
              Dim k As String = e.Result
      
      
              MessageBox.Show(k)
      
      
              busyIndicator.IsBusy = False
      
          End Sub
      

      我真的很感谢 ken2k。 stackoverflow 上的链接是link

      Thanq 可以被视为封闭式问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-04
        • 2013-04-21
        • 1970-01-01
        • 1970-01-01
        • 2017-05-25
        • 1970-01-01
        • 2018-11-04
        • 2015-08-04
        相关资源
        最近更新 更多