【问题标题】:ManualResetEvent is not working with WCF and silverlight [duplicate]ManualResetEvent 不适用于 WCF 和 silverlight [重复]
【发布时间】:2012-10-24 07:19:22
【问题描述】:

可能重复:
Why does ManualResetEvent fail to work in this synchronous call using Silverlight 4?

我在 MainPage.Xaml.cs 中有以下代码

 ManualResetEvent wait = new ManualResetEvent(false);
 Service1Client wcf = new Service1Client();
 wcf.DoWorkCompleted += (o, ev) =>
 {
   int s = (int)ev.Result;
   wait.Set();
 };
 wcf.DoWorkAsync();
 wait.WaitOne();

//My other part of code where I'd like the value of `int s`.
....

Service1.svc.cs 代码如下。

public class Service1 : IService1
{
  public int DoWork()
  {
    return 5;
  }
}

直到DoWork 完成,我希望我的代码等待,所以我编写了这段代码。虽然在 WaitOne 指令 (Service1.svc.cs) 之后方法 DoWork() 根本不会被调用。应用程序将留在那里只是不做任何事情。我以前在 SilverLight 4 的另一台机器上做过这个,它按预期工作。现在我正在使用 SilverLight 3。

【问题讨论】:

    标签: c# multithreading wcf silverlight


    【解决方案1】:

    您可能通过调用wait.WaitOne() 在UI 线程上造成死锁

    异步操作完成后,它会尝试通过 UI 调度程序调用 DoWorkCompleted。虽然它会成功地将调用排队,但它永远不会出队,因为 UI 线程在 WaitOne() 上被阻塞。

    在我看来,您应该避免这种解决方法,而只进行异步调用。

    【讨论】:

    • 艾伦我创建了全新的应用程序并尝试了同样的方法仍然无法正常工作。我正在使用导航应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多