Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

How can I turn this code snippet into a blocking function?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jamie Risk - 27 Sep 2007 20:52 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 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.