ManualResetEvent不适用于WCF和silverlight

时间:2012-10-24 07:19:23

标签: c# multithreading wcf silverlight

  

可能重复:
  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。

1 个答案:

答案 0 :(得分:0)

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

异步操作完成后,它会尝试通过UI调度程序调用{​​{1}}。虽然它会成功地对调用进行排队,但它永远不会出列,因为在DoWorkCompleted上阻止了UI线程。

在我看来,你应该避免这种解决方法,只是做异步调用。