views:

124

answers:

4

I have this:

public class ServiceLibrary
{
    public object result = null;
    private bool finished = false;

    public void testar()
    {
       ServiceReference.Service1SoapClient serviceReference = new ServiceReference.Service1SoapClient();
       serviceReference.updateUserCompleted += new EventHandler<ServiceReference.updateUserCompletedEventArgs>(serviceReference_updateUserCompleted);
       serviceReference.updateUserAsync();
       ManualResetEvent m = new ManualResetEvent(true);

    }

    void serviceReference_updateUserCompleted(object sender, ServiceReference.updateUserCompletedEventArgs e)
    {
        result = e.Result;
        finished = true;
    }
}

and outside I have this:

public Home()
{
    InitializeComponent();

    ServiceLibrary serviceLibrary = new ServiceLibrary();
    serviceLibrary.testar();
    lblCharName.Text = Convert.ToString(serviceLibrary.result);

}

What should I do to the thread wait, so when I asign the text, it contains the value, please? Thank you

A: 

I have a similar situation. I use a technique called polling, which is exactly what it sounds like. It may or may not be appropriate for you depending on your situation.

public class ServiceLibrary
{
public object result = null;
private bool finished = false;

public void testar()
{
   ServiceReference.Service1SoapClient serviceReference = new ServiceReference.Service1SoapClient();
   serviceReference.updateUserCompleted += new EventHandler<ServiceReference.updateUserCompletedEventArgs>(serviceReference_updateUserCompleted);
   serviceReference.updateUserAsync();
   ManualResetEvent m = new ManualResetEvent(true);

while !finished
 Thread.Sleep(100);

doStuffWithResult(result);
}

void serviceReference_updateUserCompleted(object sender, ServiceReference.updateUserCompletedEventArgs e)
{
    result = e.Result;
    finished = true;
}
Rice Flour Cookies
It's not clear that the caller wants to force the service call to be synchronous (which is what your change would do).
LBushkin
What else could he have meant by saying that he wants it to wait until the value is available?
Rice Flour Cookies
(-1) should never use a sleep for this kind of thing. Just wastes the users and the processors time.
Mike
A: 

Make your ManualResetEvent variable m a member variable of the class. and inside your threaded method: serviceReference_updateUserCompleted, make sure to call m.WaitOne();

C Johnson
+1  A: 
Lewray
this m thing is great, but didnt work! =Xi'm using Windows Phone 7 emulator...
Alan
I know... the `ManualResetEvent` is REALLY usefull.... I'm not sure why it wouldn't have worked. Did you make sure you set the initial state to `false`?
Lewray
This is the way to go. Not sure why it didn't work for you, but this is the standard way of achieving this.
Mike
Guys, when I call serviceLibrary.manualResetEvent.WaitOne(); outside, then it waits forever...yes the initial state is false.Any ideia?
Alan
I used another thing to work, I passed as argument to the method, so I could change what I wanted,
Alan
A: 

What about

public class ServiceLibrary
{
  public object result = null;

  public void testar()
  {
     var serviceReference = new ServiceReference.Service1SoapClient();
     using(var m = new ManualResetEvent(false))
     {
       Action<object, ServiceReference.updateUserCompletedEventArgs> handler = 
       (sender, e) => 
       {
         result = e.Result;
         m.Set();
       };
       serviceReference.updateUserCompleted += handler;
       serviceReference.updateUserAsync();
       m.WaitOne();
       serviceReference.updateUserCompleted -= handler;
     }
  }
}
Arne