How can I rewrite 'getSpecialData()' more so that it more elegantly waits
for the handler to complete?
- Jamie
class someClass {
private class SpecialData { ... }
private SpecialData specialData;
private bool blocking;
private void retrievedHandler(SpecialData d)
{ specialData = d; blocking = false; }
public SpecialData getSpecialData()
{
Retriever rtvr = new Retriever(new
DataRetrievedHandler(retrievedHandler));
blocking = true;
rtvr.fetch();
while(blocking) {}
return this.specialData;
}
}
Ashot Geodakov - 27 Sep 2007 21:11 GMT
> How can I rewrite 'getSpecialData()' more so that it more elegantly waits
> for the handler to complete?
>
> - Jamie
class someClass
{
private class SpecialData { ... }
private SpecialData specialData;
private ManualResetEvent m_event = new ManualResetEvent( false );
private void retrievedHandler(SpecialData d)
{
specialData = d;
m_event.Set();
}
public SpecialData getSpecialData()
{
Retriever rtvr = new Retriever(
new DataRetrievedHandler(retrievedHandler));
m_event.Reset();
rtvr.fetch();
m_event.WaitOne();
return this.specialData;
}
}
Jesse Houwing - 27 Sep 2007 22:04 GMT
Hello Jamie,
> How can I rewrite 'getSpecialData()' more so that it more elegantly
> waits for the handler to complete?
[quoted text clipped - 17 lines]
> }
> }
Just wondering... Why? If you're offloading processing to another thread,
why block the one you're currently in... It doesn't make sense to me. Why
not just retrieve the data synchronously. It would be much easier and in
the end you'll get the same result.
--
Jesse Houwing
jesse.houwing at sogeti.nl