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 / .NET Framework / Compact Framework / January 2006

Tip: Looking for answers? Try searching our database.

How to turn an asynchronous method into a synchronous one

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sam - 11 Jan 2006 06:55 GMT
I want to write an synchronous method Logon(). The method will need to make
asynchronous calls - sending windows messages to other apps and waiting for
replies etc etc etc. I'm wondering what the best way write it as a
synchronous method is.

I can easily write a loop which goes: Thread.Sleep(awhile) until some state
has been changed by event handlers, but is that the best way?
Sergey Bogdanov - 11 Jan 2006 13:45 GMT
You have to use ManualResetEvent for such things. Initiate event and
pass it to an asynchronous method. When a method complete job it have to
invoke Set() method. While an asynchronous method completing execution,
a synchronosu method should invoke WaitOne(). Here is pseudo code for you:

AsyncLogon(ManualResetEvent event)
{
   try
   {
      ...
   }
   finally
   {
      event.Set();
   }
}

SyncLogon()
{
   ManualResetEvent event = new ManualResetEvent(false);
   AsyncLogon(event);
   event.WaitOne();
   event.Close();
}

HTH

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com

> I want to write an synchronous method Logon(). The method will need to make
> asynchronous calls - sending windows messages to other apps and waiting for
[quoted text clipped - 3 lines]
> I can easily write a loop which goes: Thread.Sleep(awhile) until some state
> has been changed by event handlers, but is that the best way?

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.